mc.mod.php 62 KB

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