UserService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\PreOrder;
  4. use fast\Random;
  5. use GuzzleHttp\Client;
  6. use think\Db;
  7. use think\Exception;
  8. use think\Log;
  9. class UserService
  10. {
  11. private $model = null;
  12. /**
  13. * 初始化方法
  14. */
  15. public function __construct()
  16. {
  17. $this->model = Db::name('user');
  18. }
  19. /**
  20. * 用户换绑
  21. * @return void
  22. */
  23. public function userBindCompany($params=[])
  24. {
  25. $result = [
  26. 'status' => 1,
  27. 'msg' => '操作成功',
  28. 'data' => [],
  29. ];
  30. try {
  31. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  32. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  33. $where['id'] = $userId;
  34. $user = Db::name('user')->where($where['id'])->find();
  35. if (!empty($user)) {
  36. if ($user['company_id'] != $companyId) {
  37. $userData['company_id'] = $companyId;
  38. $userData['updatetime'] = time();
  39. $userRes = Db::name('user')->where($where)->update($userData);
  40. if (!$userRes) {
  41. throw new Exception('操作失败');
  42. }
  43. }
  44. }
  45. } catch (Exception $e) {
  46. $result['status'] = 0;
  47. $result['msg'] = $e->getMessage();
  48. }
  49. return $result;
  50. }
  51. /**
  52. * 用户绑定门店和钱包
  53. * @return void
  54. */
  55. public function userWallet($params=[])
  56. {
  57. $result = [
  58. 'status' => 1,
  59. 'msg' => '操作成功',
  60. 'data' => [],
  61. ];
  62. try {
  63. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  64. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  65. $comefrom = isset($params['comefrom']) ? $params['comefrom'] : '';
  66. $bindRes = $this->userBindCompany($params);//绑定门店
  67. if (!$bindRes['status']) {
  68. throw new Exception($bindRes['msg']);
  69. }
  70. $where['user_id'] = $userId;
  71. $where['company_id'] = $companyId;
  72. $userWallet = Db::name('user_wallet')->where($where)->find();
  73. $time = time();
  74. if (empty($userWallet)) {
  75. $userWalletData = [
  76. 'user_id' => $userId,
  77. 'company_id' => $companyId,
  78. 'staff_id' => 0,
  79. 'money' => 0.00,
  80. 'address' => '',
  81. 'createtime' => $time,
  82. 'comefrom' => $comefrom,
  83. 'remark' => '',
  84. ];
  85. $userWalletRes = Db::name('user_wallet')->insertGetId($userWalletData);
  86. if (!$userWalletRes) {
  87. throw new Exception('生成钱包失败');
  88. }
  89. }
  90. } catch (Exception $e) {
  91. $result['status'] = 0;
  92. $result['msg'] = $e->getMessage();
  93. }
  94. return $result;
  95. }
  96. /**
  97. * 预约消息
  98. * @return void
  99. */
  100. public function msgPreOrder($params=[])
  101. {
  102. $result = [
  103. 'status' => 1,
  104. 'msg' => '操作成功',
  105. 'data' => [],
  106. ];
  107. try {
  108. $preOrderId = isset($params['pre_order_id']) ? $params['pre_order_id'] : 0;
  109. $p = 'pre_order';
  110. $u = 'user';
  111. $c = 'company';
  112. $s = 'servicetype';
  113. $field = $p.'.*,'.$u.'.mini_openid,'.$c.'.name as `company_name`,'.$s.'.title as `service_title`';
  114. $where[$p.'.id'] = $preOrderId;
  115. $preOrder = Db::name($p)->alias($p)->field($field)
  116. ->join($u,$u.'.id = '.$p.'.user_id','LEFT')
  117. ->join($c,$c.'.id = '.$p.'.company_id','LEFT')
  118. ->join($s,$s.'.id = '.$p.'.servicetype_id','LEFT')
  119. ->where($where)->find();
  120. if (empty($preOrder)) {
  121. throw new Exception('未找到预约信息');
  122. }
  123. $statusArr = model('PreOrder')->getPreOrderStatusList();
  124. $statusText = isset($statusArr[$preOrder['pre_order_status']]) ? $statusArr[$preOrder['pre_order_status']] : '';
  125. $wechatTemplate = config('param.wechat_template');
  126. $preOrderMsg = isset($wechatTemplate['pre_order']) ? $wechatTemplate['pre_order'] : [];
  127. $wechatSetting = config('param.wechat_setting');
  128. if ($wechatSetting == 'release') {//正式
  129. /*预约时间:{{time2.DATA}}
  130. 网点名称: {{thing3.DATA}}
  131. 类型: {{thing1.DATA}}*/
  132. $data = [//数据内容
  133. "time2" => ["value" => date('Y年m月d日 H:i',$preOrder['pre_time'])],
  134. "thing3" => ["value" => $preOrder['company_name'] .'的预约'.$statusText],
  135. "thing1" => ["value" => $preOrder['service_title']],
  136. ];
  137. } else {
  138. /*预约日期 {{time1.DATA}}
  139. 场地{{thing8.DATA}}
  140. 预约类型{{thing7.DATA}}
  141. 顾客称号{{thing3.DATA}}*/
  142. $data = [//数据内容
  143. "time1" => ["value" => date('Y年m月d日 H:i',$preOrder['pre_time'])],
  144. "thing8" => ["value" => $preOrder['company_name'] .'的预约'.$statusText],
  145. "thing7" => ["value" => $preOrder['service_title']],
  146. "thing3" => ["value" => $preOrder['name']],
  147. ];
  148. }
  149. $paramsData = $preOrderMsg;
  150. $paramsData['mini_openid'] = $preOrder['mini_openid'];
  151. $paramsData['data'] = $data;
  152. $msgRes = $this->wechatMessageSend($paramsData);
  153. if (!$msgRes['status']) {
  154. throw new Exception($msgRes['msg']);
  155. }
  156. } catch (Exception $e) {
  157. $result['status'] = 0;
  158. $result['msg'] = $e->getMessage();
  159. $errorData = [
  160. 'pre_order_id' => $preOrderId,
  161. 'params' => isset($paramsData) ? $paramsData : [],
  162. 'error' => $result['msg'],
  163. ];
  164. Log::error(json_encode($errorData));
  165. }
  166. return $result;
  167. }
  168. /**
  169. * 订单消息
  170. * @return void
  171. */
  172. public function msgOrder($params=[])
  173. {
  174. $result = [
  175. 'status' => 1,
  176. 'msg' => '操作成功',
  177. 'data' => [],
  178. ];
  179. try {
  180. $orderId = isset($params['order_id']) ? $params['order_id'] : 0;
  181. $o = 'order';
  182. $u = 'user';
  183. $where[$o.'.id'] = $orderId;
  184. $field = $o.'.*,'.$u.'.mini_openid';
  185. $order = Db::name('order')->alias($o)->field($field)
  186. ->join($u,$u.'.id = '.$o.'.user_id','LEFT')
  187. ->where($where)->find();
  188. if (empty($order)) {
  189. throw new Exception('未找到订单信息');
  190. }
  191. $statusArr = model('Order')->getStatusList();
  192. $statusText = isset($statusArr[$order['status']]) ? $statusArr[$order['status']] : '';
  193. $wechatTemplate = config('param.wechat_template');
  194. $orderMsg = isset($wechatTemplate['order']) ? $wechatTemplate['order'] : [];
  195. $wechatSetting = config('param.wechat_setting');
  196. if ($wechatSetting == 'release') {//正式
  197. /*工单号: {{character_string5.DATA}}
  198. 状态: {{phrase4.DATA}}
  199. 下单时间:{{time8.DATA}}*/
  200. $data = [//数据内容
  201. "character_string5" => ["value" => $order['orderno']],
  202. "phrase4" => ["value" => $statusText],
  203. "time8" => ["value" => date('Y年m月d日 H:i:s',$order['createtime'])],
  204. ];
  205. } else {
  206. /*工单号{{character_string1.DATA}}
  207. 处理进度 {{phrase4.DATA}}
  208. 提交时间 {{time2.DATA}}*/
  209. $data = [//数据内容
  210. "character_string1" => ["value" => $order['orderno']],
  211. "phrase4" => ["value" => $statusText],
  212. "time2" => ["value" => date('Y年m月d日 H:i:s',$order['createtime'])],
  213. ];
  214. }
  215. $orderMsg['page'] = $orderMsg['page'].'?id='.$order['id'];
  216. $paramsData = $orderMsg;
  217. $paramsData['mini_openid'] = $order['mini_openid'];
  218. $paramsData['data'] = $data;
  219. $msgRes = $this->wechatMessageSend($paramsData);
  220. if (!$msgRes['status']) {
  221. throw new Exception($msgRes['msg']);
  222. }
  223. } catch (Exception $e) {
  224. $result['status'] = 0;
  225. $result['msg'] = $e->getMessage();
  226. $errorData = [
  227. 'order_id' => $orderId,
  228. 'params' => isset($paramsData) ? $paramsData : [],
  229. 'error' => $result['msg'],
  230. ];
  231. Log::error(json_encode($errorData));
  232. }
  233. return $result;
  234. }
  235. /**
  236. * 微信消息发送
  237. * @return void
  238. */
  239. public function wechatMessageSend($params=[])
  240. {
  241. $result = [
  242. 'status' => 1,
  243. 'msg' => '操作成功',
  244. 'data' => [],
  245. ];
  246. return $result;
  247. try {
  248. $client = new Client();
  249. $tk = getAccessToken();
  250. $uri = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$tk;
  251. $jsonData = [
  252. "touser" => isset($params['mini_openid']) ? $params['mini_openid'] : '',//openid
  253. "template_id" => isset($params['template_id']) ? $params['template_id'] : '',//模版ID
  254. "page" => isset($params['page']) ? $params['page'] : '', //跳转地址
  255. "miniprogram_state" => isset($params['miniprogram_state']) ? $params['miniprogram_state'] : '',//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  256. "lang" => isset($params['lang']) ? $params['lang'] : 'zh_CN', //支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
  257. "data" => isset($params['data']) ? $params['data'] : [], //数据
  258. ];
  259. $res = $client->request('POST', $uri, ['json' => $jsonData]);
  260. $returnResJson = $res->getBody()->getContents();
  261. $returnRes = json_decode($returnResJson, true);
  262. if ($returnRes['errcode'] != 0) {
  263. throw new Exception($returnRes['errmsg']);
  264. }
  265. } catch (Exception $e) {
  266. $result['status'] = 0;
  267. $result['msg'] = $e->getMessage();
  268. }
  269. return $result;
  270. }
  271. /**
  272. * 套餐卡券购买
  273. * @return array
  274. */
  275. public function packageCoupons($params=[])
  276. {
  277. $result = [
  278. 'status' => 1,
  279. 'msg' => '',
  280. 'data' => [],
  281. ];
  282. try {
  283. $packageId = isset($params['package_id']) ? $params['package_id'] : 0;
  284. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  285. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  286. $payOrderId= isset($params['payorder_id']) ? $params['payorder_id'] : 0;
  287. //赠送优惠券
  288. $pg = 'package_gift';
  289. $c = 'coupons';
  290. $field = $pg.'.*,'.$c.'.name,'.$c.'.info,'.$c.'.days';
  291. $packageGiftWhere['package_id'] = $packageId;
  292. $packageGiftWhere[$c.'.status'] = 1;
  293. $rechargeGift = Db::name($pg)->alias($pg)->field($field)
  294. ->join($c,$c.'.id = '.$pg.'.coupon_id','LEFT')->where($packageGiftWhere)->select();
  295. $time = time();
  296. foreach ($rechargeGift as $k => $v) {
  297. $checkCode[] = Random::alnum(8);
  298. }
  299. $orderService = new OrderService();//防重复
  300. $checkCodeRes = $orderService->getCheckCoupons($checkCode);
  301. foreach ($rechargeGift as $key => $value) {
  302. $endtime = $value['days'] == 0 ? 0 : $time + 86400 * $value['days'];
  303. if ($checkCodeRes['status']) {
  304. $checkCodeStr = isset($checkCodeRes['data'][$key]) ? $checkCodeRes['data'][$key] : '';
  305. } else {
  306. $checkCodeStr = Random::alnum(8);
  307. }
  308. $userCouponsData[] = [
  309. 'check_code' => $checkCodeStr,
  310. 'user_id' => $userId,
  311. 'company_id' => $companyId,
  312. 'coupons_id' => $value['coupon_id'],
  313. 'coupon_name' => $value['name'],
  314. 'coupon_info' => $value['info'],
  315. 'createtime' => $time,
  316. 'endtime' => $endtime,
  317. 'number' => $value['number'],
  318. 'remain' => $value['number'],
  319. 'payorder_id' => $payOrderId,
  320. 'getfrom' => '购买套餐',
  321. ];
  322. }
  323. $userCouponsRes = Db::name('user_coupons')->insertAll($userCouponsData);
  324. if (!$userCouponsRes) {
  325. throw new Exception('套餐券获取失败');
  326. }
  327. } catch (Exception $e) {
  328. $result['status'] = 0;
  329. $result['msg'] = $e->getMessage();
  330. }
  331. return $result;
  332. }
  333. }