UserService.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\common\service;
  3. use think\Db;
  4. class UserService
  5. {
  6. private $model = null;
  7. /**
  8. * 初始化方法
  9. */
  10. public function __construct()
  11. {
  12. $this->model = Db::name('user');
  13. }
  14. /**
  15. * 用户换绑
  16. * @return void
  17. */
  18. public function userBindCompany($params=[])
  19. {
  20. $result = [
  21. 'status' => 1,
  22. 'msg' => '操作成功',
  23. 'data' => [],
  24. ];
  25. try {
  26. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  27. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  28. $where['id'] = $userId;
  29. $user = Db::name('user')->where($where['id'])->find();
  30. if (!empty($user)) {
  31. if ($user['company_id'] != $companyId) {
  32. $userData['company_id'] = $companyId;
  33. $userData['updatetime'] = time();
  34. $userRes = Db::name('user')->where($where['id'])->update($userData);
  35. if (!$userRes) {
  36. throw new Exception('操作失败');
  37. }
  38. }
  39. }
  40. } catch (Exception $e) {
  41. $result['status'] = 0;
  42. $result['msg'] = $e->getMessage();
  43. }
  44. return $result;
  45. }
  46. /**
  47. * 用户绑定门店和钱包
  48. * @return void
  49. */
  50. public function userWallet($params=[])
  51. {
  52. $result = [
  53. 'status' => 1,
  54. 'msg' => '操作成功',
  55. 'data' => [],
  56. ];
  57. try {
  58. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  59. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  60. $comefrom = isset($params['comefrom']) ? $params['comefrom'] : '';
  61. $bindRes = $this->userBindCompany($params);//绑定门店
  62. if (!$bindRes['status']) {
  63. throw new Exception($bindRes['msg']);
  64. }
  65. $where['user_id'] = $userId;
  66. $where['company_id'] = $companyId;
  67. $userWallet = Db::name('user_wallet')->where($where['id'])->find();
  68. $time = time();
  69. if (empty($userWallet)) {
  70. $userWalletData = [
  71. 'user_id' => $userId,
  72. 'company_id' => $companyId,
  73. 'money' => 0.00,
  74. 'createtime' => $time,
  75. 'comefrom' => $comefrom,
  76. ];
  77. $userWalletRes = Db::name('user_wallet')->insertGetId($userWalletData);
  78. if (!$userWalletRes) {
  79. throw new Exception('生成钱包失败');
  80. }
  81. }
  82. } catch (Exception $e) {
  83. $result['status'] = 0;
  84. $result['msg'] = $e->getMessage();
  85. }
  86. return $result;
  87. }
  88. }