Companys.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. class Companys extends Api
  6. {
  7. protected $noNeedLogin = ['getList','getInfo'];
  8. protected $noNeedRight = '*';
  9. protected $model = null;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->model = Db::name('company');
  14. }
  15. /**
  16. * 列表
  17. * @return void
  18. */
  19. public function getList()
  20. {
  21. try {
  22. $page = $this->request->param('page',1);
  23. $limit = $this->request->param('listrow',15);
  24. $offset = ($page - 1) * $limit;
  25. //获取门店信息
  26. $params = [
  27. 'lng' => $this->request->param('lng',''),
  28. 'lat' => $this->request->param('lat',''),
  29. ];
  30. $companyList = $this->getCompanySort($params);
  31. $result = array_slice($companyList['data'],$offset,$limit);
  32. $this->success('获取成功',$result);
  33. } catch (Exception $e) {
  34. $this->error($e->getMessage());
  35. }
  36. }
  37. /**
  38. * 获取门店距离排序
  39. * @return void
  40. */
  41. public function getCompanySort($params=[])
  42. {
  43. try {
  44. $result = [
  45. 'status' => true,
  46. 'msg' => '获取成功',
  47. 'data' => [],
  48. ];
  49. $lng = isset($params['lng']) ? $params['lng'] : '';
  50. $lat = isset($params['lat']) ? $params['lat'] : '';
  51. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  52. $where['status'] = 1;
  53. $data = $this->model->where($where)->field($field)->select();
  54. if (!empty($data)) {
  55. foreach ($data as $key => &$value) {
  56. $value['image'] = !empty($value['image']) ? cdnurl($value['image']) : '';
  57. $value['distance'] = getDistance($lng, $lat, $value['longitude'], $value['latitude'], true, 2);
  58. $value['distance_text'] = !empty($value['distance']) ? $value['distance'].'km' : '';
  59. }
  60. $n = count($data);
  61. for($i=1;$i<$n;$i++) { //从第二个元素开始插入
  62. for($j=$i-1;$j>=0;$j--) { //与前面的数比较,找到插入的位置
  63. if($data[$j]['distance'] > $data[$j+1]['distance']) { //比前面的数小,交换位置
  64. $tmp = $data[$j];
  65. $data[$j] = $data[$j+1];
  66. $data[$j+1] = $tmp;
  67. } else { //大于或等于前面的数,表示已找到插入的位置
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. $result['data'] = $data;
  74. return $result;
  75. } catch (Exception $e) {
  76. $result['status'] = 0;
  77. $result['msg'] = $e->getMessage();
  78. return $result;
  79. }
  80. }
  81. /**
  82. * 详情
  83. * @return void
  84. */
  85. public function getInfo()
  86. {
  87. try {
  88. $id = $this->request->param('id',0);
  89. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  90. $where['status'] = 1;
  91. $where['id'] = $id;
  92. $result = $this->model->where($where)->field($field)->find();
  93. if (!empty($result)) {
  94. !empty($result['image']) && $result['image'] = cdnurl($result['image']);
  95. }
  96. $this->success('获取成功',$result);
  97. } catch (Exception $e) {
  98. $this->error($e->getMessage());
  99. }
  100. }
  101. /**
  102. * 绑定门店
  103. * @return void
  104. */
  105. public function bind()
  106. {
  107. Db::startTrans();
  108. try {
  109. $id = $this->request->param('id',0);
  110. $comeform = $this->request->param('comeform','');//'自然进店','平台引流','线下新客','老带新'
  111. $where['id'] = $id;
  112. $where['status'] = 1;
  113. $company = $this->model->where($where)->find();
  114. if (empty($company)) {
  115. throw new Exception('未找到门店信息');
  116. }
  117. $user = $this->auth->getUser();
  118. $userId = $this->auth->id;
  119. if ($user['company_id'] != $id) {//更新绑定门店
  120. $userWhere['id'] = $userId;
  121. $userData['company_id'] = $id;
  122. $userRes = model('User')->where($userWhere)->update($userData);
  123. if (!$userRes) {
  124. throw new Exception('绑定门店失败');
  125. }
  126. }
  127. //用户门店钱包
  128. $walletWhere['user_id'] = $userId;
  129. $walletWhere['company_id'] = $id;
  130. $userWallet = model('UserWallet')->where($walletWhere)->find();
  131. if (empty($userWallet)) {
  132. $walletData = [
  133. 'user_id' => $userId,
  134. 'company_id' => $id,
  135. 'money' => 0.00,
  136. 'comefrom' => $comeform,
  137. ];
  138. $walletRes = model('UserWallet')->insertGetId($walletData);
  139. if (!$walletRes) {
  140. throw new Exception('绑定失败');
  141. }
  142. }
  143. Db::commit();
  144. $this->success();
  145. } catch (Exception $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. }
  149. }
  150. /**
  151. * 保存
  152. * @return void
  153. */
  154. public function save()
  155. {
  156. try {
  157. //验证参数
  158. $id = $this->request->param('id',0);
  159. $userId = $this->auth->id;
  160. $scene = !empty($id) ? 'edit' : 'add';
  161. $validate = validate('Companys');
  162. if(!$validate->check($this->request->param(),[],$scene)){
  163. throw new Exception($validate->getError());
  164. }
  165. if (!empty($id)) {
  166. $where['user_id'] = $userId;
  167. $where['id'] = $id;
  168. $companyData = $this->model->where($where)->find();
  169. if (empty($companyData)) {
  170. throw new Exception('未找到相关信息');
  171. }
  172. } else {
  173. $where['user_id'] = $userId;
  174. $companyData = $this->model->where($where)->find();
  175. if (!empty($companyData)) {
  176. throw new Exception('您已申请过入驻');
  177. }
  178. }
  179. $time = time();
  180. $areaData = getProvince($this->request->param());
  181. $fullAddress = $areaData['full_address'];
  182. $data = [
  183. 'image' => '/assets/img/avatar.png',//默认头像
  184. 'contacts' => $this->request->param('contacts', ''),
  185. 'mobile' => $this->request->param('mobile', ''),
  186. 'province_id' => $this->request->param('province_id', 0),
  187. 'city_id' => $this->request->param('city_id', 0),
  188. 'area_id' => $this->request->param('area_id', 0),
  189. 'province_name' => $areaData['province_name'],
  190. 'city_name' => $areaData['city_name'],
  191. 'area_name' => $areaData['area_name'],
  192. 'address' => $this->request->param('address', ''),
  193. 'full_address' => $fullAddress,
  194. 'aptitude_images' => $this->request->param('aptitude_images', ''),
  195. 'status' => 1,
  196. ];
  197. if (empty($id)) {
  198. $data['user_id'] = $userId;
  199. $data['createtime'] = $time;
  200. $res = $this->model->insertGetId($data);
  201. } else {
  202. $data['updatetime'] = $time;
  203. $where['id'] = $id;
  204. $where['user_id'] = $userId;
  205. $res = $this->model->where($where)->update($data);
  206. }
  207. if (!$res) {
  208. throw new Exception('操作失败');
  209. }
  210. $this->success('操作成功');
  211. } catch (Exception $e) {
  212. $this->error($e->getMessage());
  213. }
  214. }
  215. }