UserController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. namespace shop\controllers;
  3. use biz\admin\services\AdminService;
  4. use biz\custom\services\CustomService;
  5. use biz\merchant\services\MerchantAssetService;
  6. use biz\merchant\services\MerchantRemarkService;
  7. use biz\user\services\UserAssetService;
  8. use biz\user\services\UserGrowthService;
  9. use biz\user\services\UserService;
  10. use biz\user\services\UserUpgradeService;
  11. use common\components\wxUtil;
  12. use common\services\xhMerchantExtendService;
  13. use common\services\xhRechargeService;
  14. use common\services\xhTMessageService;
  15. use common\services\xhUserIntegralService;
  16. use Yii;
  17. use common\components\stringUtil;
  18. use common\components\util;
  19. use biz\merchant\services\MerchantExtendService;
  20. use biz\message\services\SmsService;
  21. use biz\user\classes\UserClass;
  22. use yii\helpers\Json;
  23. class UserController extends BaseController
  24. {
  25. //客户列表 shish 2019.11.30
  26. public function actionList()
  27. {
  28. $get = Yii::$app->request->get();
  29. $search = isset($get['search']) ? $get['search'] : '';
  30. $where = ['sjId' => $this->sjId];
  31. if (!empty($search)) {
  32. if (stringUtil::isMobile($search)) {
  33. $where['mobile'] = $search;
  34. } else {
  35. $where['name'] = ['like', $search];
  36. }
  37. }
  38. //$list = CustomService::getOldCustomList($where);
  39. $list = UserService::getUserList($where);
  40. util::success($list);
  41. }
  42. //获取客户基本和资产信息 shish 2019.11.30
  43. public function actionDetail()
  44. {
  45. $userId = Yii::$app->request->get('userId');
  46. $info = UserService::getUserInfo($userId);
  47. if (isset($info['merchantId']) == false || $this->sjId != $info['merchantId']) {
  48. util::fail('没有权限');
  49. }
  50. util::success($info);
  51. }
  52. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  53. public function actionUserDetail()
  54. {
  55. $userId = Yii::$app->request->get('userId');
  56. $info = UserService::getFullUserInfo($userId);
  57. UserService::valid($info, $this->sjId);
  58. $list = MerchantRemarkService::getRemarkList($userId);
  59. $info['remarkList'] = $list;
  60. util::success($info);
  61. }
  62. //添加客户,申请会员时会自动同步资产 shish 2019.11.30
  63. public function actionAdd()
  64. {
  65. $post = Yii::$app->request->post();
  66. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  67. util::fail('请输入手机号');
  68. }
  69. if ($post['mobile'] != $post['confirmMobile']) {
  70. util::fail('二次输入手机号不一样');
  71. }
  72. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  73. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  74. $mobile = $post['mobile'];
  75. $user = UserService::getByMobile($mobile, $this->sjId);
  76. if (!empty($user)) {
  77. util::fail('手机号已使用');
  78. }
  79. $post['merchantId'] = $this->sjId;
  80. $post['member'] = $post['memberLevel'];
  81. UserService::addUser($post);
  82. util::complete();
  83. }
  84. //充值 shish 2019.12.1
  85. public function actionRecharge()
  86. {
  87. $post = Yii::$app->request->post();
  88. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  89. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  90. $userId = isset($post['userId']) ? $post['userId'] : 0;
  91. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  92. $user = UserService::getById($userId);
  93. if (empty($user)) {
  94. util::fail('找不到客户');
  95. }
  96. if ($user['merchantId'] != $this->sjId) {
  97. util::fail('您没有权限操作');
  98. }
  99. $userAsset = UserAssetService::getByUserId($userId);
  100. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  101. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  102. $upData['balance'] = $totalBalance;
  103. $upData['totalRecharge'] = $totalRecharge;
  104. $merchantAsset = MerchantAssetService::getByMerchantId($this->sjId);
  105. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->sj, $this->sjExtend, $merchantAsset);
  106. if ($return['code'] == 'A0001') {
  107. util::complete();
  108. }
  109. util::fail();
  110. }
  111. //升级 shish 2019.12.1
  112. public function actionUpgrade()
  113. {
  114. $post = Yii::$app->request->post();
  115. $userId = isset($post['userId']) ? $post['userId'] : 0;
  116. $member = isset($post['member']) ? $post['member'] : 0;
  117. $user = UserService::getById($userId);
  118. if (empty($user)) {
  119. util::fail('客户不存在');
  120. }
  121. //验证是否商家客户
  122. UserService::valid($user, $this->sjId);
  123. //验证确认密码
  124. $confirmPassword = $post['confirmPassword'];
  125. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  126. $grade = xhMerchantExtendService::getGradeList($this->sjExtend, 'desc');
  127. //升级后的成长值
  128. $newLevelGrowth = $grade[$member]['growth'];
  129. $newDiscount = $grade[$member]['discount'];
  130. $userAsset = UserAssetService::getByUserId($userId);
  131. if ($userAsset['member'] >= $member) {
  132. util::fail('客户已经达到此级别了');
  133. }
  134. $now = time();
  135. $date = date("Y-m-d H:i", $now);
  136. $connection = Yii::$app->db;//事务处理
  137. $transaction = $connection->beginTransaction();
  138. try {
  139. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  140. $upData['growth'] = $newLevelGrowth;
  141. $upData['member'] = $member;
  142. $upData['discount'] = $newDiscount;
  143. UserAssetService::updateByUserId($userId, $upData);
  144. UserService::updateById($userId, ['member' => $member]);
  145. $upgradeData = [
  146. 'member' => $member,
  147. 'prevMember' => $userAsset['member'],
  148. 'merchantId' => $this->sjId,
  149. 'gainType' => 0,
  150. 'addTime' => $now,
  151. 'userId' => $userId,
  152. ];
  153. $upgradeRespond = UserUpgradeService::add($upgradeData);
  154. $growthData = [
  155. 'userId' => $userId,
  156. 'userName' => $user['userName'],
  157. 'merchantId' => $this->sjId,
  158. 'relateId' => $upgradeRespond['id'],
  159. 'growth' => $newLevelGrowth,
  160. 'num' => $addGrowth,
  161. 'io' => 1,
  162. 'payWay' => 4,
  163. 'alipayId' => '',
  164. 'event' => '管理员操作升到' . $member . '级增加成长值',
  165. 'operatorId' => $userId,
  166. 'createTime' => $date,
  167. 'gainType' => 0,
  168. 'addTime' => $now,
  169. ];
  170. UserGrowthService::add($growthData);
  171. $transaction->commit();
  172. } catch (Exception $e) {
  173. $transaction->rollBack();
  174. util::fail();
  175. }
  176. $allTM = xhTMessageService::getList($this->sjId);
  177. $openId = isset($user['openId']) ? $user['openId'] : '';
  178. if (empty($openId)) {
  179. util::complete();
  180. }
  181. $shortTempId = 'OPENTM205211943';//升级通知
  182. if (isset($allTM[$shortTempId]) == false) {
  183. util::complete();
  184. }
  185. $tempId = $allTM[$shortTempId];
  186. $data = [
  187. "touser" => $openId, "template_id" => $tempId, "url" => '',
  188. "data" => [
  189. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  190. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  191. "keyword2" => ["value" => $date, "color" => "#173177"],
  192. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  193. ]];
  194. wxUtil::sendTaskInform($data, $this->sj);
  195. util::complete();
  196. }
  197. //重置客户密码
  198. public function actionResetPayPassword()
  199. {
  200. $post = Yii::$app->request->post();
  201. $userId = isset($post['userId']) ? $post['userId'] : 0;
  202. $password = isset($post['password']) ? $post['password'] : 0;
  203. $info = UserService::getById($userId);
  204. UserService::valid($info, $this->sjId);
  205. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  206. util::complete();
  207. }
  208. //获取登陆客户的资产 shish 2019.11.22
  209. public function actionAsset()
  210. {
  211. $asset = UserAssetService::getByUserId($this->adminId);
  212. util::success($asset);
  213. }
  214. //获取登陆客户的优惠情况 shish 2019.12.3
  215. public function actionDiscount()
  216. {
  217. $discount = UserAssetService::getDiscount($this->adminId);
  218. util::success($discount);
  219. }
  220. //小程序用户完整信息 shish 2019.12.12
  221. public function actionMiniFullInfo()
  222. {
  223. $post = Yii::$app->request->post();
  224. $iv = isset($post['iv']) ? $post['iv'] : '';
  225. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  226. $appId = $this->sj['miniAppId'];
  227. $merchantId = $this->sjId;
  228. $miniOpenId = $this->user['miniOpenId'];
  229. if (empty($miniOpenId)) {
  230. util::fail('mini_open_id empty');
  231. }
  232. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  233. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  234. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  235. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  236. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  237. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  238. if ($errCode != 0) {
  239. Yii::info($result . ' ' . $errCode);
  240. util::fail('获取用户信息失败');
  241. }
  242. Yii::info('小程序获取用户信息:' . $result);
  243. $originalInfo = Json::decode($result);
  244. $source = UserClass::$userSourceId['mini']['name'];
  245. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  246. $userId = $user['id'];
  247. $userInfo = UserService::getUserInfo($userId);
  248. util::success($userInfo);
  249. }
  250. //小程序用户手机号
  251. public function actionMiniMobile()
  252. {
  253. $post = Yii::$app->request->post();
  254. $iv = $post['iv'];
  255. $encryptedData = $post['encryptedData'];
  256. $merchantId = $this->sjId;
  257. $merchant = $this->sj;
  258. $appId = $merchant['miniAppId'];
  259. $miniOpenId = $this->user['miniOpenId'];
  260. $user = $this->user;
  261. if (!empty($user) && !empty($user['mobile'])) {
  262. $userId = $user['id'];
  263. util::success(['mobile' => $user['mobile']]);
  264. }
  265. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  266. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  267. if (empty($sessionKey)) {
  268. util::fail('系统错误,sesstion empty');
  269. }
  270. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  271. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  272. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  273. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  274. if ($errCode != 0) {
  275. util::fail('解密失败');
  276. }
  277. $arr = Json::decode($result);
  278. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  279. if (empty($mobile)) {
  280. util::fail('没有获取手机号,请重试');
  281. }
  282. $userId = $user['id'];
  283. UserService::becomeMember($user, $merchant, ['mobile' => $mobile]);
  284. util::success(['mobile' => $mobile]);
  285. }
  286. //密码修改
  287. public function actionPassword()
  288. {
  289. $post = Yii::$app->request->post();
  290. $old = isset($post['old']) ? $post['old'] : '';
  291. $new = isset($post['new']) ? $post['new'] : '';
  292. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  293. if (empty($new)) {
  294. util::fail('请输入新密码');
  295. }
  296. if ($new != $confirm) {
  297. util::fail('二次密码不一致');
  298. }
  299. $userId = $this->adminId;
  300. $user = UserService::getUserInfo($userId, false);
  301. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  302. if (empty($old)) {
  303. util::fail('请填写旧密码');
  304. }
  305. if (password_verify($old, $user['payPassword']) == false) {
  306. util::fail('旧密码错误');
  307. }
  308. }
  309. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  310. util::complete('修改成功');
  311. }
  312. //申请会员 shish 2019.12.18
  313. public function actionApplyMember()
  314. {
  315. $post = Yii::$app->request->post();
  316. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  317. if (stringUtil::isMobile($mobile) == false) {
  318. util::fail('请输入手机号');
  319. }
  320. $userId = $this->adminId;
  321. $asset = UserAssetService::getByUserId($userId);
  322. if (isset($asset['member']) && $asset['member'] != -1) {
  323. util::fail('已经是会员了');
  324. }
  325. $code = isset($post['code']) ? $post['code'] : '';
  326. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  327. if (empty($cacheCode) || $cacheCode != $code) {
  328. util::fail('验证码错误');
  329. }
  330. $update = [];
  331. if (isset($post['realName']) && !empty($post['realName'])) {
  332. $update['realName'] = $post['realName'];
  333. }
  334. if (isset($post['birthday']) && !empty($post['birthday'])) {
  335. $update['birthday'] = $post['birthday'];
  336. }
  337. $user = UserService::getByCondition(['merchantId' => $this->sjId, 'mobile' => $mobile]);
  338. if ($userId == $user['id']) {
  339. util::fail('手机号已验证');
  340. }
  341. if (!empty($user)) {
  342. util::fail('手机号已经注册过了');
  343. }
  344. $update['mobile'] = $mobile;
  345. UserService::becomeMember($this->user, $this->sj, $update);
  346. util::complete('注册成功');
  347. }
  348. //用户基本信息和资产 shish 2019.12.19
  349. public function actionMyProfile()
  350. {
  351. $user = UserService::getUserInfo($this->adminId);
  352. $growth = $user['userAsset']['growth'];
  353. $extend = $this->sjExtend;
  354. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  355. //下个等级的积分
  356. $nextLevelGrowth = 0;
  357. foreach ($grade as $val) {
  358. $currentGrowth = $val['growth'];
  359. if ($currentGrowth > $growth) {
  360. $nextLevelGrowth = $currentGrowth;
  361. break;
  362. }
  363. }
  364. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  365. util::success($user);
  366. }
  367. //登陆客户的信息 shish 2020.3.12
  368. public function actionLoginDetail()
  369. {
  370. $user = $this->user;
  371. util::success($user);
  372. }
  373. }