WechatUser.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\models\user;
  8. use app\models\store\StoreCouponIssue;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\WechatService;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\services\SystemConfigService;
  13. use app\models\routine\RoutineQrcode;
  14. use app\models\store\StoreCouponUser;
  15. use app\models\store\StoreCouponIssueUser;
  16. use think\facade\Db;
  17. /**
  18. * TODO 微信用户Model
  19. * Class WechatUser
  20. * @package app\models\user
  21. */
  22. class WechatUser extends BaseModel
  23. {
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'uid';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'wechat_user';
  34. use ModelTrait;
  35. /**
  36. * uid获取小程序Openid
  37. * @param string $uid
  38. * @return bool|mixed
  39. */
  40. public static function getOpenId($uid = '')
  41. {
  42. if ($uid == '') return false;
  43. return self::where('uid', $uid)->value('routine_openid');
  44. }
  45. /**
  46. * TODO 用uid获得openid
  47. * @param $uid
  48. * @param string $openidType
  49. * @return mixed
  50. * @throws \Exception
  51. */
  52. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  53. {
  54. $openid = self::where('uid', $uid)->value($openidType);
  55. return $openid;
  56. }
  57. /**
  58. * TODO 用openid获得uid
  59. * @param $openid
  60. * @param string $openidType
  61. * @return mixed
  62. */
  63. public static function openidTouid($openid, $openidType = 'openid')
  64. {
  65. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  66. }
  67. /**
  68. * 小程序创建用户后返回uid
  69. * @param $routineInfo
  70. * @return mixed
  71. */
  72. public static function routineOauth($routine)
  73. {
  74. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  75. $routineInfo['sex'] = $routine['gender'];//性别
  76. $routineInfo['language'] = $routine['language'];//语言
  77. $routineInfo['city'] = $routine['city'];//城市
  78. $routineInfo['province'] = $routine['province'];//省份
  79. $routineInfo['country'] = $routine['country'];//国家
  80. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  81. $routineInfo['routine_openid'] = $routine['openId'];//openid
  82. $routineInfo['session_key'] = $routine['session_key'];//会话密匙
  83. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  84. $routineInfo['user_type'] = 'routine';//用户类型
  85. $spid = 0;//绑定关系uid
  86. $isCOde = false;
  87. //获取是否有扫码进小程序
  88. if ($routine['code']) {
  89. if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
  90. $spid = $info['third_id'];
  91. $isCOde = true;
  92. } else
  93. $spid = $routine['spid'];
  94. } else if ($routine['spid'])
  95. $spid = $routine['spid'];
  96. // 判断unionid 存在根据unionid判断
  97. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  98. self::edit($routineInfo, $uid, 'uid');
  99. $routineInfo['code'] = $spid;
  100. $routineInfo['isPromoter'] = $isCOde;
  101. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  102. User::updateWechatUser($routineInfo, $uid);
  103. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  104. self::edit($routineInfo, $uid, 'uid');
  105. $routineInfo['code'] = $spid;
  106. $routineInfo['isPromoter'] = $isCOde;
  107. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  108. User::updateWechatUser($routineInfo, $uid);
  109. } else {
  110. $routineInfo['add_time'] = date("Y-m-d H:i:s");//用户添加时间
  111. $routineInfo['uid'] = self::getkeytoid('uid');
  112. $routineInfo = self::create($routineInfo);
  113. $res = User::setRoutineUser($routineInfo, $spid);
  114. $uid = $res->uid;
  115. }
  116. return $uid;
  117. }
  118. /**
  119. * 判断是否是小程序用户
  120. * @param int $uid
  121. * @return bool|int|string
  122. */
  123. public static function isRoutineUser($uid = 0)
  124. {
  125. if (!$uid) return false;
  126. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  127. }
  128. /**
  129. * @param int $uid
  130. * @return int
  131. */
  132. public static function isUserStatus($uid = 0)
  133. {
  134. if (!$uid) return 0;
  135. $user = User::getUserInfo($uid);
  136. return $user['status'];
  137. }
  138. /**
  139. * .添加新用户
  140. * @param $openid
  141. * @return object
  142. */
  143. public static function setNewUser($openid)
  144. {
  145. $userInfo = WechatService::getUserInfo($openid);
  146. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  147. exception('请关注公众号!');
  148. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  149. //判断 unionid 是否存在
  150. if (isset($userInfo['unionid'])) {
  151. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  152. unset($userInfo['qr_scene'], $userInfo['qr_scene_str'], $userInfo['qr_scene_str'], $userInfo['subscribe_scene']);
  153. if ($wechatInfo) {
  154. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  155. }
  156. }
  157. self::beginTrans();
  158. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  159. if (!$wechatUser) {
  160. self::rollbackTrans();
  161. exception('用户储存失败!');
  162. }
  163. if (!User::setWechatUser($wechatUser)) {
  164. self::rollbackTrans();
  165. exception('用户信息储存失败!');
  166. }
  167. self::commitTrans();
  168. self::userFirstSubGiveCoupon($openid);
  169. return $wechatUser;
  170. }
  171. /**
  172. * TODO 关注送优惠券
  173. * @param $openid
  174. */
  175. public static function userFirstSubGiveCoupon($openid)
  176. {
  177. $couponIds = StoreCouponIssue::where('status', 1)
  178. ->where('is_give_subscribe', 1)
  179. ->where('is_del', 0)
  180. ->column('id,cid');
  181. if ($couponIds)
  182. foreach ($couponIds as $couponId)
  183. if ($couponId) {
  184. $uid = self::openidToUid($openid);
  185. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  186. StoreCouponIssueUser::addUserIssue($uid, $couponId['id']);
  187. }
  188. }
  189. /**
  190. * 订单金额达到预设金额赠送优惠卷
  191. * @param $uid
  192. */
  193. public static function userTakeOrderGiveCoupon($uid, $total_price)
  194. {
  195. $couponIds = StoreCouponIssue::where('status', 1)
  196. ->where('is_full_give', 1)
  197. ->where('is_del', 0)
  198. ->where('status', 1)
  199. ->where('start_time', '<=', time())
  200. ->where('end_time', '>=', time())
  201. ->column('id,cid,full_reduction,remain_count,is_permanent');
  202. if ($couponIds) {
  203. $couponIssueIds = StoreCouponIssueUser::where('uid', $uid)
  204. ->whereIn('issue_coupon_id', array_column($couponIds, 'id'))
  205. ->column('issue_coupon_id');
  206. foreach ($couponIds as $couponId)
  207. if ($couponId && !in_array($couponId['id'], $couponIssueIds))
  208. if ($total_price >= $couponId['full_reduction'] && (!$couponId['is_permanent'] || $couponId['remain_count'] >= 1)) {
  209. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  210. StoreCouponIssueUser::addUserIssue($uid, $couponId['id']);
  211. StoreCouponIssue::where('id', $couponId['id'])->update(['remain_count' => $couponId['remain_count'] - 1]);
  212. }
  213. }
  214. }
  215. /**
  216. * 更新用户信息
  217. * @param $openid
  218. * @return bool
  219. */
  220. public static function updateUser($openid)
  221. {
  222. $userInfo = WechatService::getUserInfo($openid);
  223. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  224. return self::edit($userInfo->toArray(), $openid, 'openid');
  225. }
  226. /**
  227. * 用户存在就更新 不存在就添加
  228. * @param $openid
  229. */
  230. public static function saveUser($openid)
  231. {
  232. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  233. $info = self::where('openid', $openid)->find();
  234. @file_put_contents('sub2.txt', json_encode($info), FILE_APPEND);
  235. if ($info && $info['subscribe'] == 1 && $info['send'] == 0) {
  236. $subscribe_give_integral = sys_config('subscribe_give_integral', 0, true);
  237. if ($subscribe_give_integral > 0) {
  238. $mark = '首次关注,赠送积分' . $subscribe_give_integral;
  239. UserBill::income('关注赠送', $info['uid'], 'integral', 'subscribe_give', $subscribe_give_integral, 0, bcadd($info['integral'], $subscribe_give_integral, 2), $mark);
  240. User::where('uid', $info['uid'])->inc('integral', $subscribe_give_integral)->update();
  241. $info->send = 1;
  242. $info->save();
  243. }
  244. }
  245. }
  246. /**
  247. * 用户取消关注
  248. * @param $openid
  249. * @return bool
  250. */
  251. public static function unSubscribe($openid)
  252. {
  253. return self::edit(['subscribe' => 0], $openid, 'openid');
  254. }
  255. /**
  256. * TODO 用uid获得Unionid
  257. * @param $uid
  258. * @return mixed
  259. */
  260. public static function uidToUnionid($uid)
  261. {
  262. return self::where('uid', $uid)->value('unionid');
  263. }
  264. /**
  265. * TODO 获取用户信息
  266. * @param $openid
  267. * @param $openidType
  268. * @return array
  269. * @throws \think\db\exception\DataNotFoundException
  270. * @throws \think\db\exception\ModelNotFoundException
  271. * @throws \think\exception\DbException
  272. */
  273. public static function getWechatInfo($openid, $openidType)
  274. {
  275. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  276. $wechatInfo = self::where($openidType, $openid)->find();
  277. if (!$wechatInfo) {
  278. self::setNewUser($openid);
  279. $wechatInfo = self::where($openidType, $openid)->find();
  280. }
  281. if (!$wechatInfo) exception('获取用户信息失败!');
  282. return $wechatInfo->toArray();
  283. }
  284. }