123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- class Companys extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = Db::name('company');
- }
- /**
- * 列表
- * @return void
- */
- public function getList()
- {
- try {
- $page = $this->request->param('page',1);
- $limit = $this->request->param('listrow',15);
- $offset = ($page - 1) * $limit;
- //获取门店信息
- $params = [
- 'lng' => $this->request->param('lng',''),
- 'lat' => $this->request->param('lat',''),
- ];
- $companyList = $this->getCompanySort($params);
- $result = array_slice($companyList['data'],$offset,$limit);
- $this->success('获取成功',$result);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
-
- /**
- * 获取门店距离排序
- * @return void
- */
- public function getCompanySort($params=[])
- {
- try {
- $result = [
- 'status' => true,
- 'msg' => '获取成功',
- 'data' => [],
- ];
- $lng = isset($params['lng']) ? $params['lng'] : '';
- $lat = isset($params['lat']) ? $params['lat'] : '';
- $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
- $where['status'] = 1;
- $data = $this->model->where($where)->field($field)->select();
- if (!empty($data)) {
- foreach ($data as $key => &$value) {
- $value['image'] = !empty($value['image']) ? cdnurl($value['image']) : '';
- $value['distance'] = getDistance($lng, $lat, $value['longitude'], $value['latitude'], true, 2);
- $value['distance_text'] = !empty($value['distance']) ? $value['distance'].'km' : '';
- }
- $n = count($data);
- for($i=1;$i<$n;$i++) { //从第二个元素开始插入
- for($j=$i-1;$j>=0;$j--) { //与前面的数比较,找到插入的位置
- if($data[$j]['distance'] > $data[$j+1]['distance']) { //比前面的数小,交换位置
- $tmp = $data[$j];
- $data[$j] = $data[$j+1];
- $data[$j+1] = $tmp;
- } else { //大于或等于前面的数,表示已找到插入的位置
- break;
- }
- }
- }
- }
- $result['data'] = $data;
- return $result;
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- return $result;
- }
- }
- /**
- * 详情
- * @return void
- */
- public function getInfo()
- {
- try {
- $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
- $where['status'] = 1;
- $result = $this->model->where($where)->field($field)->select();
- if (!empty($result)) {
- !empty($result['image']) && $result['image'] = cdnurl($result['image']);
- }
- $this->success('获取成功',$result);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 绑定门店
- * @return void
- */
- public function bind()
- {
- Db::startTrans();
- try {
- $id = $this->request->param('id',0);
- $where['id'] = $id;
- $where['status'] = 1;
- $company = $this->model->where($where)->find();
- if (empty($company)) {
- throw new Exception('未找到门店信息');
- }
- $user = $this->auth->getUser();
- $userId = $this->auth->id;
- if ($user['company_id'] != $id) {//更新绑定门店
- $userWhere['id'] = $userId;
- $userData['company_id'] = $id;
- $userRes = model('User')->where($userWhere)->update($userData);
- if (!$userRes) {
- throw new Exception('绑定门店失败');
- }
- }
- //用户门店钱包
- $walletWhere['user_id'] = $userId;
- $walletWhere['company_id'] = $id;
- $userWallet = model('UserWallet')->where($walletWhere)->find();
- if (empty($userWallet)) {
- $walletData = [
- 'user_id' => $userId,
- 'company_id' => $id,
- 'money' => 0.00,
- ];
- $walletRes = model('UserWallet')->insertGetId($walletData);
- if (!$walletRes) {
- throw new Exception('绑定失败');
- }
- }
- Db::commit();
- $this->success();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- }
|