UserController.php 15 KB

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