mc.mod.php 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. function mc_update($uid, $fields) {
  7. global $_W;
  8. if (empty($fields)) {
  9. return false;
  10. }
  11. if (is_string($uid)) {
  12. $openid = $uid;
  13. }
  14. $uid = mc_openid2uid($uid);
  15. $_W['weid'] && $fields['weid'] = $_W['weid'];
  16. $struct = array_keys(mc_fields());
  17. $struct[] = 'birthyear';
  18. $struct[] = 'birthmonth';
  19. $struct[] = 'birthday';
  20. $struct[] = 'resideprovince';
  21. $struct[] = 'residecity';
  22. $struct[] = 'residedist';
  23. $struct[] = 'groupid';
  24. $struct[] = 'salt';
  25. if (isset($fields['birth']) && !is_array($fields['birth'])) {
  26. $birth = explode('-', $fields['birth']);
  27. $fields['birth'] = array(
  28. 'year' => $birth[0],
  29. 'month' => $birth[1],
  30. 'day' => $birth[2],
  31. );
  32. }
  33. if (!empty($fields['birth'])) {
  34. $fields['birthyear'] = $fields['birth']['year'];
  35. $fields['birthmonth'] = $fields['birth']['month'];
  36. $fields['birthday'] = $fields['birth']['day'];
  37. }
  38. if (isset($fields['reside'])) {
  39. $fields['resideprovince'] = $fields['reside']['province'];
  40. $fields['residecity'] = $fields['reside']['city'];
  41. $fields['residedist'] = $fields['reside']['district'];
  42. }
  43. unset($fields['reside'], $fields['birth']);
  44. foreach ($fields as $field => $value) {
  45. if (!in_array($field, $struct) || is_array($value)) {
  46. unset($fields[$field]);
  47. }
  48. }
  49. if (!empty($fields['avatar'])) {
  50. if (strexists($fields['avatar'], 'attachment/images/global/avatars/avatar_')) {
  51. $fields['avatar'] = str_replace($_W['attachurl'], '', $fields['avatar']);
  52. }
  53. }
  54. $member = table('mc_members')->getById($uid);
  55. if (!empty($fields['email'])) {
  56. $mc_members = table('mc_members');
  57. $mc_members->searchWithUniacid(mc_current_real_uniacid());
  58. $mc_members->searchWithoutUid($uid);
  59. $mc_members->searchWithEmail($fields['email']);
  60. $emailexists = $mc_members->getcolumn('email');
  61. if ($emailexists) {
  62. unset($fields['email']);
  63. }
  64. }
  65. if (!empty($fields['mobile'])) {
  66. $mc_members = table('mc_members');
  67. $mc_members->searchWithUniacid(mc_current_real_uniacid());
  68. $mc_members->searchWithoutUid($uid);
  69. $mc_members->searchWithEmail($fields['mobile']);
  70. $mobilexists = $mc_members->getcolumn('mobile');
  71. if ($mobilexists) {
  72. unset($fields['mobile']);
  73. }
  74. }
  75. if (empty($member)) {
  76. if(empty($fields['mobile']) && empty($fields['email'])) {
  77. return false;
  78. }
  79. $fields['uniacid'] = mc_current_real_uniacid();
  80. $fields['createtime'] = TIMESTAMP;
  81. pdo_insert('mc_members', $fields);
  82. $insert_id = pdo_insertid();
  83. } else {
  84. if (!empty($fields)) {
  85. pdo_update('mc_members', $fields, array('uid' => $uid, 'uniacid' => $_W['uniacid']));
  86. }
  87. }
  88. if (!empty($openid) && empty($uid)) {
  89. table('mc_mapping_fans')->fill(array('uid' => $insert_id))->where(array('uniacid' => mc_current_real_uniacid(), 'openid' => $openid))->save();
  90. }
  91. cache_build_memberinfo($uid);
  92. return true;
  93. }
  94. function mc_fetch($uid, $fields = array()) {
  95. if (empty($uid)) {
  96. return array();
  97. }
  98. $struct = mc_fields();
  99. $struct = array_keys($struct);
  100. if (!empty($fields)) {
  101. foreach ($fields as $key => $field) {
  102. if (!in_array($field, $struct)) {
  103. unset($fields[$key]);
  104. }
  105. if ($field == 'birth') {
  106. $fields[] = 'birthyear';
  107. $fields[] = 'birthmonth';
  108. $fields[] = 'birthday';
  109. }
  110. if ($field == 'reside') {
  111. $fields[] = 'resideprovince';
  112. $fields[] = 'residecity';
  113. $fields[] = 'residedist';
  114. }
  115. }
  116. unset($fields['birth'], $fields['reside']);
  117. }
  118. $result = array();
  119. if (is_array($uid)) {
  120. foreach ($uid as $id) {
  121. $user_info = mc_fetch_one($id);
  122. if (!empty($user_info) && !empty($fields)) {
  123. foreach ($fields as $field) {
  124. $result[$id][$field] = $user_info[$field];
  125. }
  126. $result[$id]['uid'] = $id;
  127. } else {
  128. $result[$id] = $user_info;
  129. }
  130. }
  131. } else {
  132. $user_info = mc_fetch_one($uid);
  133. if (!empty($user_info) && !empty($fields)) {
  134. foreach ($fields as $field) {
  135. $result[$field] = $user_info[$field];
  136. }
  137. $result['uid'] = $uid;
  138. } else {
  139. $result = $user_info;
  140. }
  141. }
  142. return $result;
  143. }
  144. function mc_fetch_one($uid, $uniacid = 0) {
  145. global $_W;
  146. $uid = mc_openid2uid($uid);
  147. if (empty($uid)) {
  148. return array();
  149. }
  150. $cachekey = cache_system_key('memberinfo', array('uid' => $uid));
  151. $cache = cache_load($cachekey);
  152. if (!empty($cache)) {
  153. return $cache;
  154. }
  155. $params = array('uid' => $uid);
  156. $params['uniacid'] = intval($uniacid) > 0 ? intval($uniacid) : $_W['uniacid'];
  157. $result = pdo_get('mc_members', $params);
  158. if (!empty($result)) {
  159. $result['avatar'] = tomedia($result['avatar']);
  160. $result['credit1'] = floatval($result['credit1']);
  161. $result['credit2'] = floatval($result['credit2']);
  162. $result['credit3'] = floatval($result['credit3']);
  163. $result['credit4'] = floatval($result['credit4']);
  164. $result['credit5'] = floatval($result['credit5']);
  165. $result['credit6'] = floatval($result['credit6']);
  166. } else {
  167. $result = array();
  168. }
  169. cache_write($cachekey, $result);
  170. return $result;
  171. }
  172. function mc_fansinfo($openidOruid, $acid = 0, $uniacid = 0){
  173. global $_W;
  174. if (empty($openidOruid)) {
  175. return array();
  176. }
  177. if (is_numeric($openidOruid)) {
  178. $openid = mc_uid2openid($openidOruid);
  179. if (empty($openid)) {
  180. return array();
  181. }
  182. } else {
  183. $openid = $openidOruid;
  184. }
  185. $mc_mapping_fans_table = table('mc_mapping_fans');
  186. if (!empty($uniacid)) {
  187. $mc_mapping_fans_table->searchWithUniacid($uniacid);
  188. }
  189. $mc_mapping_fans_table->searchWithOpenid($openid);
  190. $fan = $mc_mapping_fans_table->get();
  191. if (!empty($fan)) {
  192. $mc_fans_tag_table = table('mc_fans_tag');
  193. $tags_info = $mc_fans_tag_table->getByOpenid($openid);
  194. if (empty($tags_info)) {
  195. if (!empty($fan['tag']) && is_string($fan['tag'])) {
  196. if (is_base64($fan['tag'])) {
  197. $fan['tag'] = @base64_decode($fan['tag']);
  198. }
  199. if (is_serialized($fan['tag'])) {
  200. $fan['tag'] = @iunserializer($fan['tag']);
  201. }
  202. if (is_array($fan['tag']) && !empty($fan['tag']['headimgurl'])) {
  203. $fan['tag']['avatar'] = tomedia($fan['tag']['headimgurl']);
  204. unset($fan['tag']['headimgurl']);
  205. if (empty($fan['nickname']) && !empty($fan['tag']['nickname'])) {
  206. $fan['nickname'] = strip_emoji($fan['tag']['nickname']);
  207. }
  208. $fan['gender'] = $fan['sex'] = $fan['tag']['sex'];
  209. $fan['avatar'] = $fan['headimgurl'] = $fan['tag']['avatar'];
  210. }
  211. } else {
  212. $fan['tag'] = array();
  213. }
  214. } else {
  215. $fan['tag'] = $tags_info;
  216. $fan['tag']['avatar'] = $tags_info['headimgurl'];
  217. if (empty($fan['nickname']) && !empty($fan['tag']['nickname'])) {
  218. $fan['nickname'] = strip_emoji($fan['tag']['nickname']);
  219. }
  220. $fan['gender'] = $fan['sex'] = $fan['tag']['sex'];
  221. $fan['avatar'] = $fan['headimgurl'] = $fan['tag']['avatar'];
  222. }
  223. }
  224. if (empty($fan) && $openid == $_W['openid'] && !empty($_SESSION['userinfo'])) {
  225. $fan['tag'] = iunserializer(base64_decode($_SESSION['userinfo']));
  226. $fan['uid'] = 0;
  227. $fan['openid'] = $fan['tag']['openid'];
  228. $fan['follow'] = 0;
  229. if (empty($fan['nickname']) && !empty($fan['tag']['nickname'])) {
  230. $fan['nickname'] = strip_emoji($fan['tag']['nickname']);
  231. }
  232. $fan['gender'] = $fan['sex'] = $fan['tag']['sex'];
  233. $fan['avatar'] = $fan['headimgurl'] = $fan['tag']['headimgurl'];
  234. $mc_oauth_fan = mc_oauth_fans($fan['openid']);
  235. if (!empty($mc_oauth_fan)) {
  236. $fan['uid'] = $mc_oauth_fan['uid'];
  237. }
  238. }
  239. return $fan;
  240. }
  241. function mc_oauth_fans($openid, $acid = 0){
  242. $mc_oauth_fans_table = table('mc_oauth_fans');
  243. if (!empty($acid)) {
  244. $mc_oauth_fans_table->searchWithAcid($acid);
  245. }
  246. $mc_oauth_fans_table->searchWithoAuthopenid($openid);
  247. $fan = $mc_oauth_fans_table->get();
  248. return $fan;
  249. }
  250. function mc_oauth_userinfo($acid = 0) {
  251. global $_W;
  252. if (isset($_SESSION['userinfo'])) {
  253. $userinfo = iunserializer(base64_decode($_SESSION['userinfo']));
  254. if (!empty($userinfo) || is_array($userinfo)) {
  255. return $userinfo;
  256. }
  257. }
  258. if ($_W['container'] != 'wechat') {
  259. return array();
  260. }
  261. $result = mc_oauth_account_userinfo();
  262. if (is_error($result)) {
  263. load()->func('tpl');
  264. include template('mc/iswxapp', TEMPLATE_INCLUDEPATH);
  265. exit;
  266. }
  267. return $result;
  268. }
  269. function mc_oauth_account_userinfo($url = '') {
  270. global $_W;
  271. if ($_W['account']->typeSign != 'account') {
  272. error(-4, '该账号非公众号类型,不支持使用该函数!');
  273. }
  274. if (empty($_W['account']['oauth'])) {
  275. return error(-1, '未指定网页授权公众号, 无法获取用户信息.');
  276. }
  277. if (empty($_W['account']['oauth']['key'])) {
  278. return error(-2, '公众号未设置 appId 或 secret.');
  279. }
  280. if (intval($_W['account']['oauth']['level']) < 4 && !in_array($_W['account']['oauth']['level'], array(ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH, ACCOUNT_TYPE_WXAPP_WORK))) {
  281. return error(-3, '公众号非认证服务号, 无法获取用户信息.');
  282. }
  283. if (!empty($_SESSION['openid']) && intval($_W['account']['level']) >= 3) {
  284. $oauth_account = WeAccount::createByUniacid();
  285. $userinfo = $oauth_account->fansQueryInfo($_SESSION['openid']);
  286. if (!is_error($userinfo) && !empty($userinfo) && is_array($userinfo) && !empty($userinfo['nickname'])) {
  287. $userinfo['nickname'] = stripcslashes($userinfo['nickname']);
  288. $userinfo['avatar'] = $userinfo['headimgurl'];
  289. $_SESSION['userinfo'] = base64_encode(iserializer($userinfo));
  290. $fan = mc_fansinfo($_SESSION['openid']);
  291. if (!empty($fan)) {
  292. $record = array(
  293. 'updatetime' => TIMESTAMP,
  294. 'nickname' => stripslashes($userinfo['nickname']),
  295. 'follow' => $userinfo['subscribe'],
  296. 'followtime' => $userinfo['subscribe_time'],
  297. 'unionid' => $userinfo['unionid'],
  298. 'tag' => base64_encode(iserializer($userinfo))
  299. );
  300. pdo_update('mc_mapping_fans', $record, array('openid' => $_SESSION['openid'], 'uniacid' => $_W['uniacid']));
  301. } else {
  302. $record = array();
  303. $record['updatetime'] = TIMESTAMP;
  304. $record['nickname'] = stripslashes($userinfo['nickname']);
  305. $record['tag'] = base64_encode(iserializer($userinfo));
  306. $record['openid'] = $_SESSION['openid'];
  307. $record['acid'] = $_W['acid'];
  308. $record['uniacid'] = $_W['uniacid'];
  309. $record['unionid'] = $userinfo['unionid'];
  310. $record['user_from'] = $_W['account']->typeSign == 'wxapp' ? 1 : 0;
  311. pdo_insert('mc_mapping_fans', $record);
  312. }
  313. if (!empty($fan['uid']) || !empty($_SESSION['uid'])) {
  314. $uid = intval($fan['uid']);
  315. if (empty($uid)) {
  316. $uid = intval($_SESSION['uid']);
  317. }
  318. $member = mc_fetch($uid, array('nickname', 'gender', 'residecity', 'resideprovince', 'nationality', 'avatar'));
  319. $record = array();
  320. if (empty($member['nickname']) && !empty($userinfo['nickname'])) {
  321. $record['nickname'] = stripslashes($userinfo['nickname']);
  322. }
  323. if (empty($member['gender']) && !empty($userinfo['sex'])) {
  324. $record['gender'] = $userinfo['sex'];
  325. }
  326. if (empty($member['residecity']) && !empty($userinfo['city'])) {
  327. $record['residecity'] = $userinfo['city'] . '市';
  328. }
  329. if (empty($member['resideprovince']) && !empty($userinfo['province'])) {
  330. $record['resideprovince'] = $userinfo['province'] . '省';
  331. }
  332. if (empty($member['nationality']) && !empty($userinfo['country'])) {
  333. $record['nationality'] = $userinfo['country'];
  334. }
  335. if (empty($member['avatar']) && !empty($userinfo['headimgurl'])) {
  336. $record['avatar'] = $userinfo['headimgurl'];
  337. }
  338. if (!empty($record)) {
  339. pdo_update('mc_members', $record, array('uid' => $uid));
  340. cache_build_memberinfo($uid);
  341. }
  342. }
  343. return $userinfo;
  344. }
  345. }
  346. $state = 'we7sid-' . $_W['session_id'];
  347. $_SESSION['dest_url'] = urlencode($_W['siteurl']);
  348. if (!empty($url)) {
  349. $_SESSION['dest_url'] = urlencode($url);
  350. }
  351. $str = '';
  352. if(uni_is_multi_acid()) {
  353. $str = "&j={$_W['acid']}";
  354. }
  355. $oauth_url = uni_account_oauth_host();
  356. $url = $oauth_url . "app/index.php?i={$_W['uniacid']}{$str}&c=auth&a=oauth&scope=userinfo";
  357. $callback = urlencode($url);
  358. $oauth_account = WeAccount::create($_W['account']['oauth']);
  359. $forward = $oauth_account->getOauthUserInfoUrl($callback, $state);
  360. header('Location: ' . $forward);
  361. exit;
  362. }
  363. function mc_require($uid, $fields, $pre = '') {
  364. global $_W, $_GPC;
  365. if (empty($fields) || !is_array($fields)) {
  366. return false;
  367. }
  368. $flipfields = array_flip($fields);
  369. if (in_array('birth', $fields) || in_array('birthyear', $fields) || in_array('birthmonth', $fields) || in_array('birthday', $fields)) {
  370. unset($flipfields['birthyear'], $flipfields['birthmonth'], $flipfields['birthday'], $flipfields['birth']);
  371. $flipfields['birthyear'] = 'birthyear';
  372. $flipfields['birthmonth'] = 'birthmonth';
  373. $flipfields['birthday'] = 'birthday';
  374. }
  375. if (in_array('reside', $fields) || in_array('resideprovince', $fields) || in_array('residecity', $fields) || in_array('residedist', $fields)) {
  376. unset($flipfields['residedist'], $flipfields['resideprovince'], $flipfields['residecity'], $flipfields['reside']);
  377. $flipfields['resideprovince'] = 'resideprovince';
  378. $flipfields['residecity'] = 'residecity';
  379. $flipfields['residedist'] = 'residedist';
  380. }
  381. $fields = array_keys($flipfields);
  382. if (!in_array('uniacid', $fields)) {
  383. $fields[] = 'uniacid';
  384. }
  385. if (!empty($pre)) {
  386. $pre .= '<br/>';
  387. }
  388. if (empty($uid)) {
  389. foreach ($fields as $field) {
  390. $profile[$field] = '';
  391. }
  392. $uniacid = $_W['uniacid'];
  393. } else {
  394. $profile = mc_fetch($uid, $fields);
  395. $uniacid = $profile['uniacid'];
  396. }
  397. $mc_member_fields = table('mc_member_fields');
  398. $mc_member_fields->searchWithUniacid($_W['uniacid']);
  399. $mc_member_fields->selectFields(array('b.field', 'b.id as fid', 'a.*'));
  400. $system_fields = $mc_member_fields->getAllFields();
  401. if (empty($system_fields)) {
  402. $system_fields = pdo_getall('profile_fields', array(), array('id', 'field', 'title'), '');
  403. }
  404. $titles = array();
  405. foreach ($system_fields as $field) {
  406. $titles[$field['field']] = $field['title'];
  407. }
  408. $message = '';
  409. $ks = array();
  410. foreach ($profile as $k => $v) {
  411. if (empty($v)) {
  412. $ks[] = $k;
  413. $message .= $system_fields[$k]['title'] . ', ';
  414. }
  415. }
  416. if (!empty($message)) {
  417. $title = '完善资料';
  418. if (checksubmit('submit')) {
  419. if (in_array('resideprovince', $fields)) {
  420. $_GPC['resideprovince'] = $_GPC['reside']['province'];
  421. $_GPC['residecity'] = $_GPC['reside']['city'];
  422. $_GPC['residedist'] = $_GPC['reside']['district'];
  423. }
  424. if (in_array('birthyear', $fields)) {
  425. $_GPC['birthyear'] = $_GPC['birth']['year'];
  426. $_GPC['birthmonth'] = $_GPC['birth']['month'];
  427. $_GPC['birthday'] = $_GPC['birth']['day'];
  428. }
  429. $record = array_elements($fields, $_GPC);
  430. if (isset($record['uniacid'])) {
  431. unset($record['uniacid']);
  432. }
  433. foreach ($record as $field => $value) {
  434. if ($field == 'gender') {
  435. continue;
  436. }
  437. if (empty($value)) {
  438. itoast('请填写完整所有资料.', referer(), 'error');
  439. }
  440. }
  441. if (empty($record['nickname']) && !empty($_W['fans']['nickname'])) {
  442. $record['nickname'] = $_W['fans']['nickname'];
  443. }
  444. if (empty($record['avatar']) && !empty($_W['fans']['tag']['avatar'])) {
  445. $record['avatar'] = $_W['fans']['tag']['avatar'];
  446. }
  447. $condition = " AND uid != {$uid} ";
  448. if (in_array('email', $fields)) {
  449. $mc_members = table('mc_members');
  450. $mc_members->searchWithUniacid(mc_current_real_uniacid());
  451. $mc_members->searchWithoutUid($uid);
  452. $mc_members->searchWithEmail(trim($record['email']));
  453. $emailexists = $mc_members->getcolumn('email');
  454. if ($emailexists) {
  455. itoast('抱歉,您填写的手机号已经被使用,请更新。', 'refresh', 'error');
  456. }
  457. }
  458. if (in_array('mobile', $fields)) {
  459. $mc_members = table('mc_members');
  460. $mc_members->searchWithUniacid(mc_current_real_uniacid());
  461. $mc_members->searchWithoutUid($uid);
  462. $mc_members->searchWithEmail(trim($record['mobile']));
  463. $mobilexists = $mc_members->getcolumn('mobile');
  464. if ($mobilexists) {
  465. itoast('抱歉,您填写的手机号已经被使用,请更新。', 'refresh', 'error');
  466. }
  467. }
  468. $insertuid = mc_update($uid, $record);
  469. if (empty($uid)) {
  470. pdo_update('mc_oauth_fans', array('uid' => $insertuid), array('oauth_openid' => $_W['openid']));
  471. pdo_update('mc_mapping_fans', array('uid' => $insertuid), array('openid' => $_W['openid']));
  472. }
  473. itoast('资料完善成功.', 'refresh', 'success');
  474. }
  475. load()->func('tpl');
  476. load()->model('activity');
  477. $filter = array();
  478. $filter['status'] = 1;
  479. $coupons = activity_coupon_owned($_W['member']['uid'], $filter);
  480. $tokens = activity_token_owned($_W['member']['uid'], $filter);
  481. $setting = uni_setting($_W['uniacid'], array('creditnames', 'creditbehaviors'));
  482. $behavior = $setting['creditbehaviors'];
  483. $creditnames = $setting['creditnames'];
  484. $credits = mc_credit_fetch($_W['member']['uid'], '*');
  485. include template('mc/require', TEMPLATE_INCLUDEPATH);
  486. exit;
  487. }
  488. return $profile;
  489. }
  490. function mc_credit_update($uid, $credittype, $creditval = 0, $log = array()) {
  491. global $_W;
  492. $creditnames = uni_setting_load('creditnames');
  493. $creditnames = $creditnames['creditnames'];
  494. $credittype = trim($credittype);
  495. $credittypes = mc_credit_types();
  496. $clerk_types = array(
  497. '1' => '线上操作',
  498. '2' => '系统后台',
  499. '3' => '店员',
  500. );
  501. if (!in_array($credittype, $credittypes)) {
  502. return error('-1', "指定的用户积分类型 “{$credittype}”不存在.");
  503. }
  504. $creditval = floatval($creditval);
  505. if (empty($creditval)) {
  506. return true;
  507. }
  508. $value = pdo_getcolumn('mc_members', array('uid' => $uid), $credittype);
  509. if ($creditval > 0 || ($value + $creditval >= 0) || $credittype == 'credit6') {
  510. pdo_update('mc_members', array($credittype => $value + $creditval), array('uid' => $uid));
  511. cache_build_memberinfo($uid);
  512. } else {
  513. return error('-1', "积分类型为“{$credittype}”的积分不够,无法操作。");
  514. }
  515. if (empty($log) || !is_array($log)) {
  516. load()->func('logging');
  517. if (!empty($GLOBALS['site']) && $GLOBALS['site'] instanceof WeModuleSite) {
  518. $log = array(
  519. $uid,
  520. $GLOBALS['site']->module['title'] . '模块内消费' . logging_implode($_GET),
  521. $GLOBALS['site']->module['name'],
  522. 0,
  523. );
  524. } elseif (!empty($GLOBALS['_GPC']['m'])) {
  525. $modules = uni_modules();
  526. $log = array(
  527. $uid,
  528. $modules[$GLOBALS['_GPC']['m']]['title'] . '模块内消费' . logging_implode($_GET),
  529. $GLOBALS['_GPC']['m'],
  530. 0,
  531. );
  532. } else {
  533. $log = array($uid, '未记录', 0, 0);
  534. }
  535. }
  536. if ($credittype == 'credit1') {
  537. $credittype_name = $creditnames['credit1']['title'];
  538. } elseif ($credittype == 'credit2') {
  539. $credittype_name = '元';
  540. }
  541. if (empty($log[1])) {
  542. if ($creditval > 0) {
  543. $log[1] = $clerk_types[$log[5]] . ': 添加' . $creditval . $credittype_name;
  544. } else {
  545. $log[1] = $clerk_types[$log[5]] . ': 减少' . -$creditval . $credittype_name;
  546. }
  547. }
  548. $clerk_type = intval($log[5]) ? intval($log[5]) : 1;
  549. $data = array(
  550. 'uid' => $uid,
  551. 'credittype' => $credittype,
  552. 'uniacid' => $_W['uniacid'],
  553. 'num' => $creditval,
  554. 'createtime' => TIMESTAMP,
  555. 'operator' => intval($log[0]),
  556. 'module' => trim($log[2]),
  557. 'clerk_id' => intval($log[3]),
  558. 'store_id' => intval($log[4]),
  559. 'clerk_type' => $clerk_type,
  560. 'remark' => $log[1],
  561. 'real_uniacid' => mc_current_real_uniacid()
  562. );
  563. pdo_insert('mc_credits_record', $data);
  564. return true;
  565. }
  566. function mc_account_change_operator($clerk_type, $store_id, $clerk_id) {
  567. global $stores, $clerks, $_W;
  568. if(empty($stores) || empty($clerks)) {
  569. $clerks = pdo_getall('activity_clerks', array('uniacid' => $_W['uniacid']), array('id', 'name'), 'id');
  570. $stores = pdo_getall('activity_stores', array('uniacid' => $_W['uniacid']), array('id', 'business_name', 'branch_name'), 'id');
  571. }
  572. $data = array(
  573. 'clerk_cn' => '',
  574. 'store_cn' => '',
  575. );
  576. if($clerk_type == 1) {
  577. $data['clerk_cn'] = '系统';
  578. } elseif($clerk_type == 2) {
  579. $data['clerk_cn'] = pdo_getcolumn('users', array('uid' => $clerk_id), 'username');
  580. } elseif($clerk_type == 3) {
  581. if (empty($clerk_id)) {
  582. $data['clerk_cn'] = '本人操作';
  583. } else {
  584. $data['clerk_cn'] = $clerks[$clerk_id]['name'];
  585. }
  586. $data['store_cn'] = $stores[$store_id]['business_name'] . ' ' . $stores[$store_id]['branch_name'];
  587. }
  588. if (empty($data['store_cn'])) {
  589. $data['store_cn'] = '暂无门店信息';
  590. }
  591. if (empty($data['clerk_cn'])) {
  592. $data['clerk_cn'] = '暂无操作员信息';
  593. }
  594. return $data;
  595. }
  596. function mc_credit_fetch($uid, $types = array()) {
  597. if (empty($types) || $types == '*') {
  598. $select = array('credit1', 'credit2', 'credit3', 'credit4', 'credit5', 'credit6');
  599. } else {
  600. $struct = mc_credit_types();
  601. foreach ($types as $key => $type) {
  602. if (!in_array($type, $struct)) {
  603. unset($types[$key]);
  604. }
  605. }
  606. $select = $types;
  607. }
  608. return pdo_get('mc_members', array('uid' => $uid), $select);
  609. }
  610. function mc_credit_types(){
  611. static $struct = array('credit1','credit2','credit3','credit4','credit5','credit6');
  612. return $struct;
  613. }
  614. function mc_groups($uniacid = 0) {
  615. global $_W;
  616. $uniacid = intval($uniacid);
  617. if (empty($uniacid)) {
  618. $uniacid = $_W['uniacid'];
  619. }
  620. return pdo_getall('mc_groups', array('uniacid' => $uniacid), array(), 'groupid', 'credit');
  621. }
  622. function mc_fans_groups($force_update = false) {
  623. global $_W;
  624. $results = table('mc_fans_groups')->getByUniacid($_W['uniacid']);
  625. $results = empty($results['groups']) ? array() : $results['groups'];
  626. if(!empty($results) && !$force_update) {
  627. return $results;
  628. }
  629. $account_api = WeAccount::createByUniacid();
  630. if (!$account_api->isTagSupported()) {
  631. return array();
  632. }
  633. $tags = $account_api->fansTagFetchAll();
  634. if (is_error($tags)) {
  635. itoast($tags['message'], '', 'error');
  636. }
  637. if (!empty($tags['tags'])) {
  638. $tags_tmp = array();
  639. foreach ($tags['tags'] as $da) {
  640. if ($da['id'] == 1) {
  641. continue;
  642. }
  643. $tags_tmp[$da['id']] = $da;
  644. }
  645. }
  646. if (empty($results)) {
  647. $data = array('acid' => $_W['acid'], 'uniacid' => $_W['uniacid'], 'groups' => iserializer($tags_tmp));
  648. pdo_insert('mc_fans_groups', $data);
  649. } else {
  650. $data = array('groups' => iserializer($tags_tmp));
  651. pdo_update('mc_fans_groups', $data, array('uniacid' => $_W['uniacid']));
  652. }
  653. return $tags_tmp;
  654. }
  655. function _mc_login($member) {
  656. global $_W;
  657. if (!empty($member) && !empty($member['uid'])) {
  658. $member = pdo_get('mc_members', array('uid' => $member['uid'], 'uniacid' => $_W['uniacid']), array('uid', 'realname', 'mobile', 'email', 'groupid', 'credit1', 'credit2', 'credit6'));
  659. if (!empty($member) && (!empty($member['mobile']) || !empty($member['email']))) {
  660. $_W['member'] = $member;
  661. $_W['member']['groupname'] = $_W['uniaccount']['groups'][$member['groupid']]['title'];
  662. $_SESSION['uid'] = $member['uid'];
  663. mc_group_update();
  664. if (empty($_W['openid'])) {
  665. $fan = mc_fansinfo($member['uid']);
  666. if (!empty($fan)) {
  667. $_SESSION['openid'] = $fan['openid'];
  668. $_W['openid'] = $fan['openid'];
  669. $_W['fans'] = $fan;
  670. $_W['fans']['from_user'] = $_W['openid'];
  671. } else {
  672. $_W['openid'] = $member['uid'];
  673. $_W['fans'] = array(
  674. 'from_user' => $member['uid'],
  675. 'follow' => 0
  676. );
  677. }
  678. }
  679. isetcookie('logout', '', -60000);
  680. return true;
  681. }
  682. }
  683. return false;
  684. }
  685. function mc_fields() {
  686. $fields = cache_load(cache_system_key('usersfields'));
  687. if (empty($fields)) {
  688. load()->model('cache');
  689. cache_build_users_struct();
  690. $fields = cache_load(cache_system_key('usersfields'));
  691. }
  692. return $fields;
  693. }
  694. function mc_acccount_fields($uniacid = 0, $is_available = true) {
  695. global $_W;
  696. $uniacid = !empty($uniacid) ? intval($uniacid) : $_W['uniacid'];
  697. $is_available = !empty($is_available) ? 1 : 0;
  698. $mc_member_fields = table('mc_member_fields');
  699. $mc_member_fields->searchWithUniacid($uniacid);
  700. $mc_member_fields->searchWithAvailable($is_available);
  701. $mc_member_fields->selectFields(array('a.title', 'b.field'));
  702. $data = $mc_member_fields->getAllFields();
  703. $fields = array();
  704. foreach($data as $row) {
  705. $fields[$row['field']] = $row['title'];
  706. }
  707. return $fields;
  708. }
  709. function mc_handsel($touid, $fromuid, $handsel, $uniacid = '') {
  710. global $_W;
  711. $touid = intval($touid);
  712. $fromuid = intval($fromuid);
  713. if (empty($uniacid)) {
  714. $uniacid = $_W['uniacid'];
  715. }
  716. $touid_exist = mc_fetch($touid, array('uniacid'));
  717. if (empty($touid_exist)) {
  718. return error(-1, '赠送积分用户不存在');
  719. }
  720. if (empty($handsel['module'])) {
  721. return error(-1, '没有填写模块名称');
  722. }
  723. if (empty($handsel['sign'])) {
  724. return error(-1, '没有填写赠送积分对象信息');
  725. }
  726. if (empty($handsel['action'])) {
  727. return error(-1, '没有填写赠送积分动作');
  728. }
  729. $credit_value = intval($handsel['credit_value']);
  730. $params = array('uniacid' => $uniacid, 'touid' => $touid, 'fromuid' => $fromuid, 'module' => $handsel['module'], 'sign' => $handsel['sign'], 'action' => $handsel['action']);
  731. $handsel_exists = pdo_get('mc_handsel', $params);
  732. if (!empty($handsel_exists)) {
  733. return error(-1, '已经赠送过积分,每个用户只能赠送一次');
  734. }
  735. $creditbehaviors = pdo_fetchcolumn('SELECT creditbehaviors FROM ' . tablename('uni_settings') . ' WHERE uniacid = :uniacid', array(':uniacid' => $uniacid));
  736. $creditbehaviors = iunserializer($creditbehaviors) ? iunserializer($creditbehaviors) : array();
  737. if (empty($creditbehaviors['activity'])) {
  738. return error(-1, '公众号没有配置积分行为参数');
  739. } else {
  740. $credittype = $creditbehaviors['activity'];
  741. }
  742. $data = array(
  743. 'uniacid' => $uniacid,
  744. 'touid' => $touid,
  745. 'fromuid' => $fromuid,
  746. 'module' => $handsel['module'],
  747. 'sign' => $handsel['sign'],
  748. 'action' => $handsel['action'],
  749. 'credit_value' => $credit_value,
  750. 'createtime' => TIMESTAMP
  751. );
  752. pdo_insert('mc_handsel', $data);
  753. $log = array($fromuid, $handsel['credit_log']);
  754. mc_credit_update($touid, $credittype, $credit_value, $log);
  755. return true;
  756. }
  757. function mc_openid2uid($openid) {
  758. global $_W;
  759. if (is_numeric($openid)) {
  760. return $openid;
  761. }
  762. if (is_string($openid)) {
  763. $fans_info = pdo_get('mc_mapping_fans', array('uniacid' => mc_current_real_uniacid(), 'openid' => $openid), array('uid'));
  764. return !empty($fans_info) ? $fans_info['uid'] : false;
  765. }
  766. if (is_array($openid)) {
  767. $uids = array();
  768. foreach ($openid as $k => $v) {
  769. if (is_numeric($v)) {
  770. $uids[] = intval($v);
  771. } elseif (is_string($v)) {
  772. $fans[] = istripslashes(str_replace(' ', '', $v));
  773. }
  774. }
  775. if (!empty($fans)) {
  776. $fans = pdo_getall('mc_mapping_fans', array('uniacid' => mc_current_real_uniacid(), 'openid' => $fans), array('uid', 'openid'), 'uid');
  777. $fans = array_keys($fans);
  778. $uids = array_merge((array)$uids, $fans);
  779. }
  780. return $uids;
  781. }
  782. return false;
  783. }
  784. function mc_uid2openid($uid) {
  785. global $_W;
  786. if (is_numeric($uid)) {
  787. $fans_info = pdo_get('mc_mapping_fans', array('uniacid' => mc_current_real_uniacid(), 'uid' => $uid), 'openid');
  788. return !empty($fans_info['openid']) ? $fans_info['openid'] : false;
  789. }
  790. if (is_string($uid)) {
  791. $openid = trim($uid);
  792. $openid_exist = pdo_get('mc_mapping_fans', array('openid' => $openid));
  793. if (!empty($openid_exist)) {
  794. return $openid;
  795. } else {
  796. return false;
  797. }
  798. }
  799. if (is_array($uid)) {
  800. $openids = array();
  801. foreach ($uid as $key => $value) {
  802. if (is_string($value)) {
  803. $openids[] = $value;
  804. } elseif (is_numeric($value)) {
  805. $uids[] = $value;
  806. }
  807. }
  808. if (!empty($uids)) {
  809. $fans_info = pdo_getall('mc_mapping_fans', array('uniacid' => mc_current_real_uniacid(), 'uid' => $uids), array('uid', 'openid'), 'openid');
  810. $fans_info = array_keys($fans_info);
  811. $openids = array_merge($openids, $fans_info);
  812. }
  813. return $openids;
  814. }
  815. return false;
  816. }
  817. function mc_group_update($uid = 0) {
  818. global $_W;
  819. if(!$_W['uniaccount']['grouplevel']) {
  820. $_W['uniaccount']['grouplevel'] = pdo_getcolumn('uni_settings', array('uniacid' => $_W['uniacid']), 'grouplevel');
  821. if (empty($_W['uniaccount']['grouplevel'])) {
  822. return true;
  823. }
  824. }
  825. $uid = intval($uid);
  826. if($uid <= 0) {
  827. $uid = $_W['member']['uid'];
  828. $user = $_W['member'];
  829. $user['openid'] = $_W['openid'];
  830. } else {
  831. $user = pdo_get('mc_members', array('uniacid' => $_W['uniacid'], 'uid' => $uid), array('uid', 'realname', 'credit1', 'credit6', 'groupid'));
  832. $user['openid'] = pdo_getcolumn('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'uid' => $uid), 'openid');
  833. }
  834. if(empty($user)) {
  835. return false;
  836. }
  837. $groupid = $user['groupid'];
  838. $credit = $user['credit1'] + $user['credit6'];
  839. $groups = mc_groups();
  840. if(empty($groups)) {
  841. return false;
  842. }
  843. $data = array();
  844. foreach($groups as $group) {
  845. $data[$group['groupid']] = $group['credit'];
  846. }
  847. asort($data);
  848. if($_W['uniaccount']['grouplevel'] == 1) {
  849. foreach($data as $k => $da) {
  850. if($credit >= $da) {
  851. $groupid = $k;
  852. }
  853. }
  854. } else {
  855. $now_group_credit = $data[$user['groupid']];
  856. if($now_group_credit < $credit) {
  857. foreach($data as $k => $da) {
  858. if($credit >= $da) {
  859. $groupid = $k;
  860. }
  861. }
  862. }
  863. }
  864. if($groupid > 0 && $groupid != $user['groupid']) {
  865. pdo_update('mc_members', array('groupid' => $groupid), array('uniacid' => $_W['uniacid'], 'uid' => $uid));
  866. cache_build_memberinfo($uid);
  867. mc_notice_group($user['openid'], $_W['uniaccount']['groups'][$user['groupid']]['title'], $_W['uniaccount']['groups'][$groupid]['title']);
  868. }
  869. $user['groupid'] = $groupid;
  870. $_W['member']['groupid'] = $groupid;
  871. $_W['member']['groupname'] = $_W['uniaccount']['groups'][$groupid]['title'];
  872. return $user['groupid'];
  873. }
  874. function mc_notice_init() {
  875. global $_W;
  876. if(empty($_W['account'])) {
  877. $_W['account'] = uni_fetch($_W['uniacid']);
  878. }
  879. if(empty($_W['account'])) {
  880. return error(1, '创建公众号操作类失败');
  881. }
  882. if($_W['account']['level'] < 3) {
  883. return error(1, '公众号没有经过认证,不能使用模板消息和客服消息');
  884. }
  885. $account = WeAccount::createByUniacid($_W['uniacid']);
  886. if(is_null($account)) {
  887. return error(1, '创建公众号操作对象失败');
  888. }
  889. $setting = uni_setting();
  890. $noticetpl = $setting['tplnotice'];
  891. $account->noticetpl = $noticetpl;
  892. return $account;
  893. }
  894. function mc_notice_public($openid, $title, $sender, $content, $url = '', $remark = '') {
  895. $account = mc_notice_init();
  896. if(is_error($account)) {
  897. return error(-1, $account['message']);
  898. }
  899. $data = array(
  900. 'first' => array(
  901. 'value' => $title,
  902. 'color' => '#ff510'
  903. ),
  904. 'keyword1' => array(
  905. 'value' => $sender,
  906. 'color' => '#ff510'
  907. ),
  908. 'keyword2' => array(
  909. 'value' => $content,
  910. 'color' => '#ff510'
  911. ),
  912. 'remark' => array(
  913. 'value' => $remark,
  914. 'color' => '#ff510'
  915. ),
  916. );
  917. $status = $account->sendTplNotice($openid, $account->noticetpl['public'], $data, $url);
  918. return $status;
  919. }
  920. function mc_notice_recharge($openid, $uid = 0, $num = 0, $url = '', $remark = '') {
  921. global $_W;
  922. if(!$uid) {
  923. $uid = $_W['member']['uid'];
  924. }
  925. if(!$uid || !$num || empty($openid)) {
  926. return error(-1, '参数错误');
  927. }
  928. $account = mc_notice_init();
  929. if(is_error($account)) {
  930. return error(-1, $account['message']);
  931. }
  932. $credit = mc_credit_fetch($uid);
  933. $time = date('Y-m-d H:i');
  934. if(empty($url)) {
  935. $url = murl('mc/bond/credits', array('credittype' => 'credit2', 'type' => 'record', 'period' => '1'), true, true);
  936. }
  937. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && !empty($account->noticetpl['recharge']['tpl'])) {
  938. $data = array(
  939. 'first' => array(
  940. 'value' => "您好,您在{$time}进行会员余额充值,充值金额{$num}元,充值后余额为{$credit['credit2']}元",
  941. 'color' => '#ff510'
  942. ),
  943. 'accountType' => array(
  944. 'value' => '会员UID',
  945. 'color' => '#ff510'
  946. ),
  947. 'account' => array(
  948. 'value' => $uid,
  949. 'color' => '#ff510'
  950. ),
  951. 'amount' => array(
  952. 'value' => $num . '元',
  953. 'color' => '#ff510'
  954. ),
  955. 'result' => array(
  956. 'value' => '充值成功',
  957. 'color' => '#ff510'
  958. ),
  959. 'remark' => array(
  960. 'value' => "{$remark}" ,
  961. 'color' => '#ff510'
  962. ),
  963. );
  964. $status = $account->sendTplNotice($openid, $account->noticetpl['recharge']['tpl'], $data, $url);
  965. }
  966. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || is_error($status) || empty($account->noticetpl['recharge']['tpl'])) {
  967. $info = "【{$_W['account']['name']}】充值通知\n";
  968. $info .= "您在{$time}进行会员余额充值,充值金额【{$num}】元,充值后余额【{$credit['credit2']}】元。\n";
  969. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  970. $custom = array(
  971. 'msgtype' => 'text',
  972. 'text' => array('content' => urlencode($info)),
  973. 'touser' => $openid,
  974. );
  975. $status = $account->sendCustomNotice($custom);
  976. }
  977. return $status;
  978. }
  979. function mc_notice_credit2($openid, $uid, $credit2_num, $credit1_num = 0, $store = '线下消费', $url = '', $remark = '谢谢惠顾,点击查看详情') {
  980. global $_W;
  981. if(!$uid) {
  982. $uid = $_W['member']['uid'];
  983. }
  984. if(!$uid || !$credit2_num || empty($openid)) {
  985. return error(-1, '参数错误');
  986. }
  987. $account = mc_notice_init();
  988. if(is_error($account)) {
  989. return error(-1, $account['message']);
  990. }
  991. $credit = mc_credit_fetch($uid);
  992. $time = date('Y-m-d H:i');
  993. if(empty($url)) {
  994. $url = murl('mc/bond/credits', array('credittype' => 'credit2', 'type' => 'record', 'period' => '1'), true, true);
  995. }
  996. $credit_setting = uni_setting_load('creditnames');
  997. $credit1_title = empty($credit_setting['creditnames']['credit1']['title']) ? '积分' : $credit_setting['creditnames']['credit1']['title'];
  998. $credit2_title = empty($credit_setting['creditnames']['credit2']['title']) ? '余额' : $credit_setting['creditnames']['credit2']['title'];
  999. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && !empty($account->noticetpl['credit2']['tpl'])) {
  1000. $data = array(
  1001. 'first' => array(
  1002. 'value' => "您好,您在{$time}有{$credit2_title}消费",
  1003. 'color' => '#ff510'
  1004. ),
  1005. 'keyword1' => array(
  1006. 'value' => abs($credit2_num) . '元',
  1007. 'color' => '#ff510'
  1008. ),
  1009. 'keyword2' => array(
  1010. 'value' => floatval($credit1_num) . $credit1_title,
  1011. 'color' => '#ff510'
  1012. ),
  1013. 'keyword3' => array(
  1014. 'value' => trim($store),
  1015. 'color' => '#ff510'
  1016. ),
  1017. 'keyword4' => array(
  1018. 'value' => $credit['credit2'] . '元',
  1019. 'color' => '#ff510'
  1020. ),
  1021. 'keyword5' => array(
  1022. 'value' => $credit['credit1'] . $credit1_title,
  1023. 'color' => '#ff510'
  1024. ),
  1025. 'remark' => array(
  1026. 'value' => "{$remark}" ,
  1027. 'color' => '#ff510'
  1028. ),
  1029. );
  1030. $status = $account->sendTplNotice($openid, $account->noticetpl['credit2']['tpl'], $data, $url);
  1031. }
  1032. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || is_error($status) || empty($account->noticetpl['credit2']['tpl'])) {
  1033. $info = "【{$_W['account']['name']}】消费通知\n";
  1034. $info .= "您在{$time}进行会员{$credit2_title}消费,消费金额【{$credit2_num}】元,获得{$credit1_title}【{$credit1_num}】,消费后余额【{$credit['credit2']}】元,消费后{$credit1_title}【{$credit['credit1']}】。\n";
  1035. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  1036. $custom = array(
  1037. 'msgtype' => 'text',
  1038. 'text' => array('content' => urlencode($info)),
  1039. 'touser' => $openid,
  1040. );
  1041. $status = $account->sendCustomNotice($custom);
  1042. }
  1043. return $status;
  1044. }
  1045. function mc_notice_credit1($openid, $uid, $credit1_num, $tip, $url = '', $remark = '谢谢惠顾,点击查看详情') {
  1046. global $_W;
  1047. if(!$uid) {
  1048. $uid = $_W['member']['uid'];
  1049. }
  1050. if(!$uid || !$credit1_num || empty($tip)) {
  1051. return error(-1, '参数错误');
  1052. }
  1053. $account = mc_notice_init();
  1054. if(is_error($account)) {
  1055. return error(-1, $account['message']);
  1056. }
  1057. $credit = mc_credit_fetch($uid);
  1058. $time = date('Y-m-d H:i');
  1059. if(empty($url)) {
  1060. $url = murl('mc/bond/credits', array('credittype' => 'credit1', 'type' => 'record', 'period' => '1'), true, true);
  1061. }
  1062. $credit1_num = floatval($credit1_num);
  1063. $type = '消费';
  1064. if($credit1_num > 0) {
  1065. $type = '到账';
  1066. }
  1067. $username = $_W['member']['realname'];
  1068. if(empty($username)) {
  1069. $username = $_W['member']['nickname'];
  1070. }
  1071. if(empty($username)) {
  1072. $username = $uid;
  1073. }
  1074. $credit_setting = uni_setting_load('creditnames');
  1075. $credit1_title = empty($credit_setting['creditnames']['credit1']['title']) ? '积分' : $credit_setting['creditnames']['credit1']['title'];
  1076. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && !empty($account->noticetpl['credit1']['tpl'])) {
  1077. $data = array(
  1078. 'first' => array(
  1079. 'value' => "您好,您在{$time}有{$credit1_title}变更",
  1080. 'color' => '#ff510'
  1081. ),
  1082. 'keyword1' => array(
  1083. 'value' => "原有{$credit1_title} : " . ($credit['credit1'] - $credit1_num),
  1084. 'color' => '#ff510'
  1085. ),
  1086. 'keyword2' => array(
  1087. 'value' => "现有{$credit1_title} : " . $credit['credit1'],
  1088. 'color' => '#ff510'
  1089. ),
  1090. 'keyword3' => array(
  1091. 'value' => "时间 : {$time} ",
  1092. 'color' => '#ff510'
  1093. ),
  1094. 'remark' => array(
  1095. 'value' => "{$remark}" ,
  1096. 'color' => '#ff510'
  1097. ),
  1098. );
  1099. $status = $account->sendTplNotice($openid, $account->noticetpl['credit1']['tpl'], $data, $url);
  1100. }
  1101. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || empty($account->noticetpl['credit1']['tpl']) || is_error($status)) {
  1102. $info = "【{$_W['account']['name']}】{$credit1_title}变更通知\n";
  1103. $info .= "您在{$time}有{$credit1_title}{$type},{$type}{$credit1_title}【{$credit1_num}】,变更原因:【{$tip}】,消费后账户{$credit1_title}余额【{$credit['credit1']}】。\n";
  1104. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  1105. $custom = array(
  1106. 'msgtype' => 'text',
  1107. 'text' => array('content' => urlencode($info)),
  1108. 'touser' => $openid,
  1109. );
  1110. $status = $account->sendCustomNotice($custom);
  1111. }
  1112. return $status;
  1113. }
  1114. function mc_notice_group($openid, $old_group, $now_group, $url = '', $remark = '点击查看详情') {
  1115. global $_W;
  1116. $account = mc_notice_init();
  1117. if(is_error($account)) {
  1118. return error(-1, $account['message']);
  1119. }
  1120. $time = date('Y-m-d H:i');
  1121. if(empty($url)) {
  1122. $url = murl('mc/home', array(), true, true);
  1123. }
  1124. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && !empty($account->noticetpl['group']['tpl'])) {
  1125. $data = array(
  1126. 'first' => array(
  1127. 'value' => "您好,您的会员组变更为{$now_group}",
  1128. 'color' => '#ff510'
  1129. ),
  1130. 'grade1' => array(
  1131. 'value' => $old_group,
  1132. 'color' => '#ff510'
  1133. ),
  1134. 'grade2' => array(
  1135. 'value' => $now_group,
  1136. 'color' => '#ff510'
  1137. ),
  1138. 'time' => array(
  1139. 'value' => $time,
  1140. 'color' => '#ff510'
  1141. ),
  1142. 'remark' => array(
  1143. 'value' => "{$remark}",
  1144. 'color' => '#ff510'
  1145. ),
  1146. );
  1147. $status = $account->sendTplNotice($openid, $account->noticetpl['group']['tpl'], $data, $url);
  1148. }
  1149. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || is_error($status) || empty($account->noticetpl['group']['tpl'])) {
  1150. $info = "【{$_W['account']['name']}】会员组变更通知\n";
  1151. $info .= "您的会员等级在{$time}由{$old_group}变更为{$now_group}。\n";
  1152. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  1153. $custom = array(
  1154. 'msgtype' => 'text',
  1155. 'text' => array('content' => urlencode($info)),
  1156. 'touser' => $openid,
  1157. );
  1158. $status = $account->sendCustomNotice($custom);
  1159. }
  1160. return $status;
  1161. }
  1162. function mc_notice_nums_plus($openid, $type, $num, $total_num, $remark = '感谢您的支持,祝您生活愉快!') {
  1163. global $_W;
  1164. if(empty($num) || empty($total_num) || empty($type)) {
  1165. return error(-1, '参数错误');
  1166. }
  1167. $account = mc_notice_init();
  1168. if(is_error($account)) {
  1169. return error(-1, $account['message']);
  1170. }
  1171. $time = date('Y-m-d H:i');
  1172. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && !empty($account->noticetpl['nums_plus']['tpl'])) {
  1173. $data = array(
  1174. 'first' => array(
  1175. 'value' => "您好,您的{$type}已充次成功",
  1176. 'color' => '#ff510'
  1177. ),
  1178. 'keyword1' => array(
  1179. 'value' => $time,
  1180. 'color' => '#ff510'
  1181. ),
  1182. 'keyword2' => array(
  1183. 'value' => $num . '次',
  1184. 'color' => '#ff510'
  1185. ),
  1186. 'keyword3' => array(
  1187. 'value' => $total_num . '次',
  1188. 'color' => '#ff510'
  1189. ),
  1190. 'keyword4' => array(
  1191. 'value' => '用完为止',
  1192. 'color' => '#ff510'
  1193. ),
  1194. 'remark' => array(
  1195. 'value' => "{$remark}" ,
  1196. 'color' => '#ff510'
  1197. ),
  1198. );
  1199. $status = $account->sendTplNotice($openid, $account->noticetpl['nums_plus']['tpl'], $data);
  1200. }
  1201. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || is_error($status) || empty($account->noticetpl['nums_plus']['tpl'])) {
  1202. $info = "【{$_W['account']['name']}】-【{$type}】充值通知\n";
  1203. $info .= "您的{$type}已充值成功,本次充次【{$num}】次,总剩余【{$total_num}】次。\n";
  1204. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  1205. $custom = array(
  1206. 'msgtype' => 'text',
  1207. 'text' => array('content' => urlencode($info)),
  1208. 'touser' => $openid,
  1209. );
  1210. $status = $account->sendCustomNotice($custom);
  1211. }
  1212. return $status;
  1213. }
  1214. function mc_notice_nums_times($openid, $card_id, $type, $num, $remark = '感谢您对本店的支持,欢迎下次再来!') {
  1215. global $_W;
  1216. if(empty($num) || empty($type) || empty($card_id)) {
  1217. return error(-1, '参数错误');
  1218. }
  1219. $account = mc_notice_init();
  1220. if(is_error($account)) {
  1221. return error(-1, $account['message']);
  1222. }
  1223. $time = date('Y-m-d H:i');
  1224. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && !empty($account->noticetpl['nums_times']['tpl'])) {
  1225. $data = array(
  1226. 'first' => array(
  1227. 'value' => "您好,您的{$type}已成功使用了【1】次。",
  1228. 'color' => '#ff510'
  1229. ),
  1230. 'keyword1' => array(
  1231. 'value' => $card_id,
  1232. 'color' => '#ff510'
  1233. ),
  1234. 'keyword2' => array(
  1235. 'value' => $time,
  1236. 'color' => '#ff510'
  1237. ),
  1238. 'keyword3' => array(
  1239. 'value' => $num . '次',
  1240. 'color' => '#ff510'
  1241. ),
  1242. 'keyword4' => array(
  1243. 'value' => '用完为止',
  1244. 'color' => '#ff510'
  1245. ),
  1246. 'remark' => array(
  1247. 'value' => "{$remark}" ,
  1248. 'color' => '#ff510'
  1249. ),
  1250. );
  1251. $status = $account->sendTplNotice($openid, $account->noticetpl['nums_times']['tpl'], $data);
  1252. }
  1253. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || is_error($status) || empty($account->noticetpl['nums_times']['tpl'])) {
  1254. $info = "【{$_W['account']['name']}】-【{$type}】消费通知\n";
  1255. $info .= "您的{$type}已成功使用了一次,总剩余【{$num}】次,消费时间【{$time}】。\n";
  1256. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  1257. $custom = array(
  1258. 'msgtype' => 'text',
  1259. 'text' => array('content' => urlencode($info)),
  1260. 'touser' => $openid,
  1261. );
  1262. $status = $account->sendCustomNotice($custom);
  1263. }
  1264. return $status;
  1265. }
  1266. function mc_notice_times_plus($openid, $card_id, $type, $fee, $days, $endtime = '', $remark = '感谢您对本店的支持,欢迎下次再来!') {
  1267. global $_W;
  1268. $account = mc_notice_init();
  1269. if(is_error($account)) {
  1270. return error(-1, $account['message']);
  1271. }
  1272. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && empty($account->noticetpl['times_plus']['tpl'])) {
  1273. $data = array(
  1274. 'first' => array(
  1275. 'value' => "您好,您的{$type}已续费成功。",
  1276. 'color' => '#ff510'
  1277. ),
  1278. 'keynote1' => array(
  1279. 'value' => $type,
  1280. 'color' => '#ff510'
  1281. ),
  1282. 'keynote2' => array(
  1283. 'value' => $card_id,
  1284. 'color' => '#ff510'
  1285. ),
  1286. 'keynote3' => array(
  1287. 'value' => $fee . '元',
  1288. 'color' => '#ff510'
  1289. ),
  1290. 'keynote4' => array(
  1291. 'value' => $days . '天',
  1292. 'color' => '#ff510'
  1293. ),
  1294. 'keynote5' => array(
  1295. 'value' => $endtime,
  1296. 'color' => '#ff510'
  1297. ),
  1298. 'remark' => array(
  1299. 'value' => "{$remark}" ,
  1300. 'color' => '#ff510'
  1301. ),
  1302. );
  1303. $status = $account->sendTplNotice($openid, $account->noticetpl['times_plus']['tpl'], $data);
  1304. }
  1305. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || is_error($status) || empty($account->noticetpl['times_plus']['tpl'])) {
  1306. $info = "【{$_W['account']['name']}】-【{$type}】续费通知\n";
  1307. $info .= "您的{$type}已成功续费,续费时长【{$days}】天,续费金额【{$fee}】元,有效期至【{$endtime}】。\n";
  1308. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  1309. $custom = array(
  1310. 'msgtype' => 'text',
  1311. 'text' => array('content' => urlencode($info)),
  1312. 'touser' => $openid,
  1313. );
  1314. $status = $account->sendCustomNotice($custom);
  1315. }
  1316. return $status;
  1317. }
  1318. function mc_notice_times_times($openid, $title, $type, $endtime = '', $remark = '请注意时间,防止服务失效!', $card_sn = '', $use_time = '', $has_time = '') {
  1319. global $_W;
  1320. $account = mc_notice_init();
  1321. if(is_error($account)) {
  1322. return error(-1, $account['message']);
  1323. }
  1324. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && !empty($account->noticetpl['times_times']['tpl'])) {
  1325. $data = array(
  1326. 'first' => array('value' => $title, 'color' => '#ff510'), 'keyword1' => array('value' => $card_sn, 'color' => '#ff510'), 'keyword2' => array('value' => $use_time, 'color' => '#ff510'), 'keyword3' => array('value' => $has_time, 'color' => '#ff510'), 'keyword4' => array('value' => $endtime, 'color' => '#ff510'), 'remark' => array('value' => "{$remark}" ,'color' => '#ff510'), );
  1327. $status = $account->sendTplNotice($openid, $account->noticetpl['times_times']['tpl'], $data);
  1328. }
  1329. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || is_error($status) || empty($account->noticetpl['times_times']['tpl'])) {
  1330. $info = "【{$_W['account']['name']}】-【{$type}】服务到期通知\n";
  1331. $info .= "您的{$type}即将到期,有效期至【{$endtime}】。\n";
  1332. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  1333. $custom = array(
  1334. 'msgtype' => 'text',
  1335. 'text' => array('content' => urlencode($info)),
  1336. 'touser' => $openid,
  1337. );
  1338. $status = $account->sendCustomNotice($custom);
  1339. }
  1340. return $status;
  1341. }
  1342. function mc_notice_pay_success($openid, $username, $order_sn, $money, $goods_info, $title = '尊敬的客户,您的订单已支付成功', $remark = '', $url = '') {
  1343. global $_W;
  1344. $money = sprintf("%.2f", $money);
  1345. if(empty($money)|| empty($openid)) {
  1346. return error(-1, '参数错误');
  1347. }
  1348. $account = mc_notice_init();
  1349. if(is_error($account)) {
  1350. return error(-1, $account['message']);
  1351. }
  1352. if($_W['account']['level'] == ACCOUNT_SERVICE_VERIFY && !empty($account->noticetpl['pay_success']['tpl'])) {
  1353. $data = array(
  1354. 'first' => array(
  1355. 'value' => $title,
  1356. 'color' => '#ff510'
  1357. ),
  1358. 'keyword1' => array(
  1359. 'value' => $username,
  1360. 'color' => '#ff510'
  1361. ),
  1362. 'keyword2' => array(
  1363. 'value' => $order_sn,
  1364. 'color' => '#ff510'
  1365. ),
  1366. 'keyword3' => array(
  1367. 'value' => $money. '元',
  1368. 'color' => '#ff510'
  1369. ),
  1370. 'keyword4' => array(
  1371. 'value' => $goods_info,
  1372. 'color' => '#ff510'
  1373. ),
  1374. 'remark' => array(
  1375. 'value' => $remark ,
  1376. 'color' => '#ff510'
  1377. ),
  1378. );
  1379. $status = $account->sendTplNotice($openid, $account->noticetpl['pay_success']['tpl'], $data, $url);
  1380. }
  1381. if($_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || is_error($status) || empty($account->noticetpl['pay_success']['tpl'])) {
  1382. $info = "【{$_W['account']['name']}】付款成功通知\n";
  1383. $info .= "您编号为{$order_sn}的订单已成功支付{$money}。\n";
  1384. $info .= "商品信息:{$goods_info}。\n";
  1385. $info .= !empty($remark) ? "备注:{$remark}\n\n" : '';
  1386. $custom = array(
  1387. 'msgtype' => 'text',
  1388. 'text' => array('content' => urlencode($info)),
  1389. 'touser' => $openid,
  1390. );
  1391. $status = $account->sendCustomNotice($custom);
  1392. }
  1393. return $status;
  1394. }
  1395. function mc_notice_consume($openid, $title, $content, $url = '') {
  1396. global $_W;
  1397. $account = mc_notice_init();
  1398. if(is_error($account)) {
  1399. return error(-1, $account['message']);
  1400. }
  1401. if($_W['account']['level'] == 4) {
  1402. mc_notice_credit2($openid, $content['uid'], $content['credit2_num'], $content['credit1_num'], $content['store'], '', $content['remark']);
  1403. }
  1404. if($_W['account']['level'] == 3) {
  1405. mc_notice_custom_text($openid, $title, $content);
  1406. }
  1407. return true;
  1408. }
  1409. function mc_notice_custom_text($openid, $title, $info) {
  1410. global $_W;
  1411. $account = mc_notice_init();
  1412. if(is_error($account)) {
  1413. return error(-1, $account['message']);
  1414. }
  1415. $custom = array(
  1416. 'msgtype' => 'text',
  1417. 'text' => array('content' => urlencode($title . '\n' . $info)),
  1418. 'touser' => $openid,
  1419. );
  1420. $status = $account->sendCustomNotice($custom);
  1421. return $status;
  1422. }
  1423. function mc_plugins() {
  1424. $plugins = array(
  1425. 'mc_card_manage' => array(
  1426. 'title' => '会员卡',
  1427. 'name' => 'mc_card_manage',
  1428. 'description' => '提供粉丝可开通会员卡并可以设置充值、消费金额及积分的增减策略',
  1429. ),
  1430. 'activity_discount_manage' => array(
  1431. 'title' => '兑换中心',
  1432. 'name' => 'activity_discount_manage',
  1433. 'description' => '提供粉丝可通过积分进行代金劵、折扣劵或是真实物品的兑换',
  1434. ),
  1435. 'wechat_card_manage' => array(
  1436. 'title' => '微信卡券',
  1437. 'name' => 'wechat_card_manage',
  1438. 'description' => '提供粉丝可通过积分进行代金劵、折扣劵或是真实物品的兑换',
  1439. ),
  1440. );
  1441. return $plugins;
  1442. }
  1443. function mc_init_fans_info($openid, $force_init_member = false){
  1444. global $_W;
  1445. static $account_api;
  1446. if (empty($account_api)) {
  1447. $account_api = WeAccount::createByUniacid();
  1448. }
  1449. if (is_array($openid)) {
  1450. $fans_list = $account_api->fansBatchQueryInfo($openid);
  1451. } else {
  1452. $fans_list = $account_api->fansQueryInfo($openid);
  1453. }
  1454. if (empty($fans_list) || is_error($fans_list)) {
  1455. if ($fans_list['errno'] == '48001') {
  1456. $fans_list = array(
  1457. 'openid' => $openid,
  1458. 'subscribe_time' => TIMESTAMP,
  1459. 'subscribe' => 1,
  1460. );
  1461. } else {
  1462. return true;
  1463. }
  1464. }
  1465. if (!is_array($openid)) {
  1466. $fans_list = array($fans_list);
  1467. }
  1468. foreach ($fans_list as $fans) {
  1469. $fans_mapping = mc_fansinfo($fans['openid']);
  1470. if (empty($fans['subscribe']) && empty($fans_mapping)) {
  1471. pdo_update('mc_mapping_fans', array('follow' => 0, 'unfollowtime' => TIMESTAMP), array('openid' => $fans['openid']));
  1472. continue;
  1473. }
  1474. if (!empty($fans_mapping) && $fans['openid'] == $fans_mapping['openid']) {
  1475. $fans = array_merge($fans_mapping['tag'], $fans);
  1476. }
  1477. unset($fans['remark'], $fans['subscribe_scene'], $fans['qr_scene'], $fans['qr_scene_str']);
  1478. $fans_update_info = array(
  1479. 'openid' => $fans['openid'],
  1480. 'acid' => $_W['acid'],
  1481. 'uniacid' => $_W['uniacid'],
  1482. 'updatetime' => TIMESTAMP,
  1483. 'followtime' => $fans['subscribe_time'],
  1484. 'follow' => $fans['subscribe'],
  1485. 'nickname' => strip_emoji(stripcslashes($fans['nickname'])),
  1486. 'tag' => '',
  1487. 'unionid' => $fans['unionid'],
  1488. 'groupid' => !empty($fans['tagid_list']) ? (','.join(',', $fans['tagid_list']).',') : '',
  1489. 'user_from' => $_W['account']->typeSign == 'wxapp' ? 1 : 0,
  1490. );
  1491. if (empty($fans_update_info['groupid'])) {
  1492. unset($fans_update_info['groupid']);
  1493. }
  1494. if ($force_init_member) {
  1495. $member_update_info = array(
  1496. 'uniacid' => $_W['uniacid'],
  1497. 'nickname' => $fans_update_info['nickname'],
  1498. 'avatar' => $fans['headimgurl'],
  1499. 'gender' => $fans['sex'],
  1500. 'nationality' => $fans['country'],
  1501. 'resideprovince' => $fans['province'] . '省',
  1502. 'residecity' => $fans['city'] . '市',
  1503. );
  1504. if (empty($fans_mapping['uid'])) {
  1505. $email = md5($fans['openid']).'@we7.cc';
  1506. $email_exists_member = pdo_getcolumn('mc_members', array('email' => $email, 'uniacid' => $_W['uniacid']), 'uid');
  1507. if (!empty($email_exists_member)) {
  1508. $uid = $email_exists_member;
  1509. } else {
  1510. $member_update_info['groupid'] = pdo_getcolumn('mc_groups', array('uniacid' => $_W['uniacid'], 'isdefault' => 1), 'groupid');
  1511. $member_update_info['salt'] = random(8);
  1512. $member_update_info['password'] = md5($fans['openid'] . $member_update_info['salt'] . $_W['config']['setting']['authkey']);
  1513. $member_update_info['email'] = $email;
  1514. $member_update_info['createtime'] = TIMESTAMP;
  1515. pdo_insert('mc_members', $member_update_info);
  1516. $uid = pdo_insertid();
  1517. }
  1518. $fans_update_info['uid'] = $uid;
  1519. } else {
  1520. $fans_update_info['uid'] = $fans_mapping['uid'];
  1521. pdo_update('mc_members', $member_update_info, array('uid' => $fans_mapping['uid']));
  1522. cache_delete(cache_system_key('memberinfo', array('uid' => $fans_mapping['uid'])));
  1523. }
  1524. }
  1525. if (!empty($fans_mapping)) {
  1526. pdo_update('mc_mapping_fans', $fans_update_info, array('fanid' => $fans_mapping['fanid']));
  1527. pdo_update('mc_fans_tag', array('fanid' => $fans_mapping['fanid']), array('openid' => $fans_mapping['openid']));
  1528. } else {
  1529. $fans_update_info['salt'] = random(8);
  1530. $fans_update_info['unfollowtime'] = 0;
  1531. $fans_update_info['followtime'] = TIMESTAMP;
  1532. pdo_insert('mc_mapping_fans', $fans_update_info);
  1533. $fans_mapping['fanid'] = pdo_insertid();
  1534. }
  1535. if (!empty($fans['tagid_list'])) {
  1536. $groupid = $fans['tagid_list'];
  1537. @sort($groupid, SORT_NATURAL);
  1538. mc_insert_fanstag_mapping($fans_mapping['fanid'], $groupid);
  1539. }
  1540. $mc_fans_tag_table = table('mc_fans_tag');
  1541. $mc_fans_tag_fields = mc_fans_tag_fields();
  1542. $fans_tag_update_info = array();
  1543. foreach ($fans as $fans_field_key => $fans_field_info) {
  1544. if (in_array($fans_field_key, array_keys($mc_fans_tag_fields))) {
  1545. $fans_tag_update_info[$fans_field_key] = $fans_field_info;
  1546. }
  1547. $fans_tag_update_info['tagid_list'] = iserializer($fans_tag_update_info['tagid_list']);
  1548. }
  1549. $fans_tag_exists = $mc_fans_tag_table->getByOpenid($fans_tag_update_info['openid']);
  1550. if (!empty($fans_tag_exists)) {
  1551. pdo_update('mc_fans_tag', $fans_tag_update_info, array('openid' => $fans_tag_update_info['openid']));
  1552. } else {
  1553. pdo_insert('mc_fans_tag', $fans_tag_update_info);
  1554. }
  1555. }
  1556. if (is_string($openid) && !empty($fans_update_info)) {
  1557. return $fans_update_info;
  1558. } else {
  1559. return true;
  1560. }
  1561. }
  1562. function mc_insert_fanstag_mapping($fanid, $groupid_list){
  1563. if (empty($groupid_list)) {
  1564. return true;
  1565. }
  1566. foreach ($groupid_list as $groupid) {
  1567. $record_mapping = array(
  1568. 'fanid' => $fanid,
  1569. 'tagid' => $groupid
  1570. );
  1571. $isfound = pdo_getcolumn('mc_fans_tag_mapping', $record_mapping, 'id');
  1572. if (empty($isfound)) {
  1573. pdo_insert('mc_fans_tag_mapping', $record_mapping);
  1574. }
  1575. }
  1576. pdo_delete('mc_fans_tag_mapping', array('fanid' => $fanid, 'tagid !=' => $groupid_list));
  1577. return true;
  1578. }
  1579. function mc_batch_insert_fanstag_mapping($fanid_list, $tagid_list){
  1580. if (!is_array($fanid_list) || !is_array($tagid_list)) {
  1581. return false;
  1582. }
  1583. $sql = '';
  1584. foreach ($fanid_list as $fanid) {
  1585. foreach ($tagid_list as $tagid) {
  1586. $fanid = intval($fanid);
  1587. $tagid = intval($tagid);
  1588. pdo_insert('mc_fans_tag_mapping', array('fanid' => $fanid, 'tagid' => $tagid), true);
  1589. }
  1590. }
  1591. return true;
  1592. }
  1593. function mc_show_tag($groupid){
  1594. if ($groupid) {
  1595. $fans_tag = mc_fans_groups();
  1596. $tagid_arr = explode(',', trim($groupid, ','));
  1597. foreach ($tagid_arr as $tagid) {
  1598. $tag_show .= $fans_tag[$tagid]['name'] . ', ';
  1599. }
  1600. $tag_show = rtrim($tag_show, ', ');
  1601. } else {
  1602. $tag_show = '无标签';
  1603. }
  1604. return $tag_show;
  1605. }
  1606. function mc_card_settings_hide($item = '') {
  1607. $mcFields = mc_acccount_fields();
  1608. if ($item == 'personal_info') {
  1609. if (empty($mcFields['idcard']) && empty($mcFields['height']) && empty($mcFields['weight']) && empty($mcFields['bloodtype']) && empty($mcFields['zodiac']) && empty($mcFields['constellation']) && empty($mcFields['site']) && empty($mcFields['affectivestatus']) && empty($mcFields['lookingfor']) && empty($mcFields['bio']) && empty($mcFields['interest'])) {
  1610. return true;
  1611. }
  1612. } elseif ($item == 'contact_method') {
  1613. if (empty($mcFields['telephone']) && empty($mcFields['qq']) && empty($mcFields['msn']) && empty($mcFields['taobao']) && empty($mcFields['alipay'])) {
  1614. return true;
  1615. }
  1616. } elseif ($item == 'education_info') {
  1617. if (empty($mcFields['education']) && empty($mcFields['graduateschool']) && empty($mcFields['studentid'])) {
  1618. return true;
  1619. }
  1620. } elseif ($item == 'jobedit') {
  1621. if (empty($mcFields['company']) && empty($mcFields['occupation']) && empty($mcFields['position']) && empty($mcFields['revenue'])) {
  1622. return true;
  1623. }
  1624. } elseif (empty($item)) {
  1625. if (empty($mcFields['idcard']) && empty($mcFields['height']) && empty($mcFields['weight'])
  1626. && empty($mcFields['bloodtype']) && empty($mcFields['zodiac']) && empty($mcFields['constellation'])
  1627. && empty($mcFields['site']) && empty($mcFields['affectivestatus']) && empty($mcFields['lookingfor'])
  1628. && empty($mcFields['bio']) && empty($mcFields['interest']) && empty($mcFields['telephone'])
  1629. && empty($mcFields['qq']) && empty($mcFields['msn']) && empty($mcFields['taobao'])
  1630. && empty($mcFields['alipay']) && empty($mcFields['education']) && empty($mcFields['graduateschool'])
  1631. && empty($mcFields['studentid']) && empty($mcFields['company']) && empty($mcFields['occupation'])
  1632. && empty($mcFields['position']) && empty($mcFields['revenue']) && empty($mcFields['avatar'])
  1633. && empty($mcFields['nickname']) && empty($mcFields['realname']) && empty($mcFields['gender'])
  1634. && empty($mcFields['birthyear']) && empty($mcFields['resideprovince'])) {
  1635. return true;
  1636. }
  1637. }
  1638. return false;
  1639. }
  1640. function mc_card_grant_credit($openid, $card_fee, $storeid = 0, $modulename) {
  1641. global $_W;
  1642. $setting = uni_setting($_W['uniacid'], array('creditbehaviors'));
  1643. load()->model('card');
  1644. $recharges_set = card_params_setting('cardRecharge');
  1645. $card_settings = card_setting();
  1646. $grant_rate = $card_settings['grant_rate'];
  1647. $grant_rate_switch = intval($recharges_set['params']['grant_rate_switch']);
  1648. $grant_credit1_enable = false;
  1649. if (!empty($grant_rate)) {
  1650. if (empty($recharges_set['params']['recharge_type'])) {
  1651. $grant_credit1_enable = true;
  1652. } else {
  1653. if ($grant_rate_switch == '1') {
  1654. $grant_credit1_enable = true;
  1655. }
  1656. }
  1657. }
  1658. if (!empty($grant_credit1_enable)) {
  1659. $num = $card_fee * $grant_rate;
  1660. $tip = "用户消费{$card_fee}元,支付金额{$card_fee},积分赠送比率为:【1:{$grant_rate}】,共赠送【{$num}】积分";
  1661. mc_credit_update($openid, 'credit1', $num, array('0', $tip, $modulename, 0, $storeid, 3));
  1662. return error(0, $num);
  1663. } else {
  1664. return error(-1, '');
  1665. }
  1666. }
  1667. function mc_current_real_uniacid() {
  1668. global $_W;
  1669. if (!empty($_W['account']['link_uniacid']) || (!empty($_W['account']) && $_W['uniacid'] != $_W['account']['uniacid'])) {
  1670. return $_W['account']['uniacid'];
  1671. } else {
  1672. return $_W['uniacid'];
  1673. }
  1674. }
  1675. function mc_parse_profile($profile) {
  1676. global $_W;
  1677. if (empty($profile)) {
  1678. return array();
  1679. }
  1680. if (!empty($profile['avatar'])) {
  1681. $profile['avatar'] = tomedia($profile['avatar']);
  1682. } else {
  1683. $profile['avatar'] = './resource/images/nopic.jpg';
  1684. }
  1685. $profile['avatarUrl'] = $profile['avatar'];
  1686. $profile['birth'] = array(
  1687. 'year' => $profile['birthyear'],
  1688. 'month' => $profile['birthmonth'],
  1689. 'day' => $profile['birthday'],
  1690. );
  1691. $profile['reside'] = array(
  1692. 'province' => $profile['resideprovince'],
  1693. 'city' => $profile['city'],
  1694. 'district' => $profile['dist']
  1695. );
  1696. if(empty($profile['email']) || (!empty($profile['email']) && substr($profile['email'], -6) == 'we7.cc' && strlen($profile['email']) == 39)) {
  1697. $profile['email'] = '';
  1698. }
  1699. return $profile;
  1700. };
  1701. function mc_member_export_parse($members, $header = array()){
  1702. if (empty($members)) {
  1703. return false;
  1704. }
  1705. $groups = mc_groups();
  1706. $keys = array_keys($header);
  1707. $html = "\xEF\xBB\xBF";
  1708. foreach ($header as $li) {
  1709. $html .= $li . "\t ,";
  1710. }
  1711. $html .= "\n";
  1712. $count = count($members);
  1713. $pagesize = ceil($count/5000);
  1714. for ($j = 1; $j <= $pagesize; $j++) {
  1715. $list = array_slice($members, ($j-1) * 5000, 5000);
  1716. if (!empty($list)) {
  1717. $size = ceil(count($list) / 500);
  1718. for ($i = 0; $i < $size; $i++) {
  1719. $buffer = array_slice($list, $i * 500, 500);
  1720. $user = array();
  1721. foreach ($buffer as $row) {
  1722. if (strexists($row['email'], 'we7.cc')) {
  1723. $row['email'] = '';
  1724. }
  1725. $row['createtime'] = date('Y-m-d H:i:s', $row['createtime']);
  1726. $row['groupid'] = $groups[$row['groupid']]['title'];
  1727. foreach ($keys as $key) {
  1728. $data[] = $row[$key];
  1729. }
  1730. $user[] = implode("\t ,", $data) . "\t ,";
  1731. unset($data);
  1732. }
  1733. $html .= implode("\n", $user) . "\n";
  1734. }
  1735. }
  1736. }
  1737. return $html;
  1738. }
  1739. function mc_fans_has_member_info($tag) {
  1740. if (is_base64($tag)) {
  1741. $tag = base64_decode($tag);
  1742. }
  1743. if (is_serialized($tag)) {
  1744. $tag = iunserializer($tag);
  1745. }
  1746. $profile = array();
  1747. if (!empty($tag)) {
  1748. if(!empty($tag['nickname'])) {
  1749. $profile['nickname'] = $tag['nickname'];
  1750. }
  1751. if(!empty($tag['sex'])) {
  1752. $profile['gender'] =$tag['sex'];
  1753. }
  1754. if(!empty($tag['province'])) {
  1755. $profile['resideprovince'] = $tag['province'];
  1756. }
  1757. if(!empty($tag['city'])) {
  1758. $profile['residecity'] = $tag['city'];
  1759. }
  1760. if(!empty($tag['country'])) {
  1761. $profile['nationality'] = $tag['country'];
  1762. }
  1763. if(!empty($tag['headimgurl'])) {
  1764. $profile['avatar'] = rtrim($tag['headimgurl']);
  1765. }
  1766. }
  1767. return $profile;
  1768. }
  1769. function mc_fans_chats_record_formate($chat_record) {
  1770. load()->model('material');
  1771. if (empty($chat_record)) {
  1772. return array();
  1773. }
  1774. foreach ($chat_record as &$record) {
  1775. if ($record['flag'] == FANS_CHATS_FROM_SYSTEM) {
  1776. $record['content'] = iunserializer($record['content']);
  1777. if (isset($record['content']['media_id']) && !empty($record['content']['media_id'])) {
  1778. $material = material_get($record['content']['media_id']);
  1779. switch($record['msgtype']) {
  1780. case 'image':
  1781. $record['content'] = tomedia($material['attachment']);
  1782. break;
  1783. case 'mpnews':
  1784. $record['content'] = $material['news'][0]['thumb_url'];
  1785. break;
  1786. case 'music':
  1787. $record['content'] = $material['filename'];
  1788. break;
  1789. case 'voice':
  1790. $record['content'] = $material['filename'];
  1791. break;
  1792. case 'voice':
  1793. $record['content'] = $material['filename'];
  1794. break;
  1795. }
  1796. } else {
  1797. $record['content'] = urldecode($record['content']['content']);
  1798. }
  1799. }
  1800. $record['createtime'] = date('Y-m-d H:i', $record['createtime']);
  1801. }
  1802. return $chat_record;
  1803. }
  1804. function mc_send_content_formate($data) {
  1805. $type = addslashes($data['type']);
  1806. if ($type == 'image') {
  1807. $contents = explode(',', htmlspecialchars_decode($data['content']));
  1808. $get_content = array_rand($contents, 1);
  1809. $content = trim($contents[$get_content], '\"');
  1810. }
  1811. if ($type == 'text' || $type == 'voice') {
  1812. $contents = htmlspecialchars_decode($data['content']);
  1813. $contents = explode(',', $contents);
  1814. $get_content = array_rand($contents, 1);
  1815. $content = trim($contents[$get_content], '\"');
  1816. }
  1817. if ($type == 'news' || $type == 'music') {
  1818. $contents = htmlspecialchars_decode($data['content']);
  1819. $contents = json_decode('[' . $contents . ']', true);
  1820. $get_content = array_rand($contents, 1);
  1821. $content = $contents[$get_content];
  1822. }
  1823. $send['touser'] = trim($data['openid']);
  1824. $send['msgtype'] = $type;
  1825. if ($type == 'text') {
  1826. $send['text'] = array('content' => urlencode(emoji_unicode_decode($content)));
  1827. } elseif ($type == 'image') {
  1828. $send['image'] = array('media_id' => $content);
  1829. $material = material_get($content);
  1830. $content = $material['attachment'];
  1831. } elseif ($type == 'voice') {
  1832. $send['voice'] = array('media_id' => $content);
  1833. } elseif($type == 'video') {
  1834. $content = json_decode($content, true);
  1835. $send['video'] = array(
  1836. 'media_id' => $content['mediaid'],
  1837. 'thumb_media_id' => '',
  1838. 'title' => urlencode($content['title']),
  1839. 'description' => ''
  1840. );
  1841. } elseif($type == 'music') {
  1842. $send['music'] = array(
  1843. 'musicurl' => tomedia($content['url']),
  1844. 'hqmusicurl' => tomedia($content['hqurl']),
  1845. 'title' => urlencode($content['title']),
  1846. 'description' => urlencode($content['description']),
  1847. 'thumb_media_id' => $content['thumb_media_id'],
  1848. );
  1849. } elseif($type == 'news') {
  1850. $send['msgtype'] = 'mpnews';
  1851. $send['mpnews'] = array(
  1852. 'media_id' => $content['mediaid']
  1853. );
  1854. }
  1855. return array(
  1856. 'send' => $send,
  1857. 'content' => $content
  1858. );
  1859. }
  1860. function mc_fans_tag_fields() {
  1861. return array(
  1862. 'subscribe' => '用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息',
  1863. 'openid' => '用户的标识,对当前公众号唯一',
  1864. 'nickname' => '用户的昵称',
  1865. 'sex' => '用户的性别,值为1时是男性,值为2时是女性,值为0时是未知',
  1866. 'city' => '用户所在城市',
  1867. 'country' => '用户所在国家',
  1868. 'province' => '用户所在省份',
  1869. 'language' => '用户的语言,简体中文为zh_CN',
  1870. 'headimgurl'=> '用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效',
  1871. 'subscribe_time' => '用户关注时间,为时间戳。如果用户曾多次关注,则取最后关注时间',
  1872. 'unionid' => '只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段',
  1873. 'remark' => '公众号运营者对粉丝的备注,公众号运营者可在微信公众平台用户管理界面对粉丝添加备注',
  1874. 'groupid' => '用户所在的分组ID(暂时兼容用户分组旧接口)',
  1875. 'tagid_list' => '用户被打上的标签ID列表',
  1876. 'subscribe_scene' => '返回用户关注的渠道来源,ADD_SCENE_SEARCH 公众号搜索,ADD_SCENE_ACCOUNT_MIGRATION 公众号迁移,ADD_SCENE_PROFILE_CARD 名片分享,ADD_SCENE_QR_CODE 扫描二维码,ADD_SCENEPROFILE LINK 图文页内名称点击,ADD_SCENE_PROFILE_ITEM 图文页右上角菜单,ADD_SCENE_PAID 支付后关注,ADD_SCENE_OTHERS 其他',
  1877. 'qr_scene' => '二维码扫码场景(开发者自定义)',
  1878. 'qr_scene_str' => '二维码扫码场景描述(开发者自定义)',
  1879. );
  1880. }