mc.mod.php 64 KB

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