public function add() { $this->data['password'] = mymd5($this->data['password'], $this->data['salt']); $email = $this->data['email']; //保存 $id = parent::add(); if ($id !== false) { //>>1.发送激活邮件... /* * 第一种方式 $uid = String::uuid(); //保持到数据库中 $fireurl = "http://www.shop.com/index.php/Member/fire/key/$uid"; */ /* * 第二种方式 */ $key = mymd5($id, "谁破解是牛逼"); $fireurl = "http://www.shop.com/index.php/Member/fire/id/{$id}/key/{$key}"; $content = <<<MAILCONTENT <h1>xxx激活邮件</h1> <p>点击<a href='{$fireurl}' target='_blank'>这里</a></p> <p>请将下面的地址复制到浏览器上</p> <p>{$fireurl}<p> MAILCONTENT; sendMail($email, 'xxx激活邮件', $content); } }
/** * 注册处理 */ public function do_register() { $username = I('username'); $password = I('password'); $repassword = I('repassword'); if (empty($username)) { $this->error('用户名不能为空'); } if (empty($password)) { $this->error('密码不能为空'); } if ($password != $repassword) { $this->error('确认密码错误'); } //检测用户是否已注册 $model = new Model('User'); $user = $model->where(array('username' => $username))->find(); if (!empty($user)) { $this->error('用户名已存在'); } $data = array('username' => $username, 'password' => md5($password), 'created_at' => time()); if (!($model->create($data) && $model->add())) { $this->error('注册失败!' . $model->getDbError()); } $this->success('注册成功,请登录', U('login')); }
public function add() { $this->startTrans(); //>>1.检查用户提交过来的数据如果为默认值, 将其他的默认去掉 if (isset($this->data['is_default'])) { $result = $this->where(array('member_id' => UID))->setField('is_default', 0); if ($result === false) { $this->rollback(); return false; } } if (!empty($this->data['id'])) { $id = $this->data['id']; //>>1.更新 $result = parent::save(); //使用this->data中的数据进行更新 if ($result === false) { $this->rollback(); return false; } } else { //>>2.保存 $id = parent::add(); // if ($id === false) { $this->rollback(); return false; } } $this->commit(); //>>3.根据id再从数据库中获取一行数据 $row = $this->find($id); //>>4.返回这一行数据 return $row; }
/** * 发送验证码 * @param string $mobile 手机号 * @return boolean 是否成功 */ public function send($mobile) { //验证码每天手机号最大数量 if (parent::where(array('mobile' => $mobile, 'create_time' => array('gt', time())))->count() > (int) C('sms.mobile_day_max')) { $this->error = '手机号 ' . $mobile . ' 已超过每天发送验证码最大数量'; return false; } //验证码每天ip最大数量 if (parent::where(array('ip' => get_client_ip(), 'create_time' => array('gt', time())))->count() > (int) C('sms.ip_day_max')) { $this->error = 'ip ' . get_client_ip() . ' 已超过每天发送验证码最大数量'; return false; } //验证码每天ip最大数量 if (time() - parent::where(array('mobile' => $mobile))->order('id desc')->getField('create_time') < (int) C('sms.send_wait_time')) { $this->error = '发送间隔过于频繁'; return false; } if (!($data = $this->create((new Sms())->send($mobile)))) { return false; } if ($data['status'] != '0') { $this->error = (new Sms())->getError($data['code']); return false; } return parent::add($data); }
/** * 添加地址进地址表 */ public function add() { if ($this->data['is_default']) { $this->where(array('member_id' => UID)); parent::save(array('is_default' => 0)); } return parent::add(); }
/** * 登录情况下的商品添加到数据库中 * @param $requestData */ public function addDb($requestData) { $row = $this->where(array('member_id' => UID, 'goods_id' => $requestData['goods_id']))->find(); if ($row) { return $this->where(array('member_id' => UID, 'goods_id' => $requestData['goods_id']))->setInc('num', $requestData['num']); } else { $data = array('member_id' => UID, 'goods_id' => $requestData['goods_id'], 'num' => $requestData['num']); return parent::add($data); } }
/** * 将购物车数据保存到shopping_car表中 * @param $requestData * @return xxx */ public function addDB($requestData) { //先查询数据库中有没有当前加入购物车的商品对应的购物车明细记录.如果有,修改该数据的num字段上的值.如果没有,新添加一天记录 $row = $this->where(array('member_id' => UID, 'goods_id' => $requestData['id']))->find(); if ($row) { return $this->where(array('goods_id' => $requestData['id'], 'member_id' => UID))->setInc('num', $requestData['num']); } else { $data = array('member_id' => UID, 'goods_id' => $requestData['id'], 'num' => $requestData['num']); return parent::add($data); } }
/** * 添加用户 */ public function add() { $email = $this->data['email']; //对用用户输入的密码进行加盐加密 $this->data['password'] = md5(md5($this->data['password']) . $this->data['salt']); $id = parent::add(); $activateKey = md5($email); //给用户发送激活邮件,传递当前注册用户的id和md5加密后的用户email,当用户点击链接,请求MemberController中activate的方法,对用户账户进行验证激活 $mail_content = " 请点击下面链接激活京西商城会员账号: <br/><br/><a href='http://www.shop.com/Member/activate/id/{$id}/email/{$activateKey}' target='_blank'> 感谢您的支持! 请使劲点我! </a>"; sendMail($email, '京西商城激活邮件', $mail_content); return $id; }
/** * @param $goodsinfo * 将商品信息添加到数据库的购物车中 */ private function addDb($goodsinfo) { $goodsinfo['member_id'] = UID; //判断是否购物车中已经存在该商品 if ($this->where("member_id={$goodsinfo['member_id']} and goods_id={$goodsinfo['goods_id']}")->count()) { //改变购物车中已经存在的商品的数量 $this->where(array('member_id' => $goodsinfo['member_id'], 'goods_id' => $goodsinfo['goods_id'])); return parent::setInc('num', $goodsinfo['num']); } else { //直接将商品加入购物车 parent::add($goodsinfo); } }
/** * 覆盖add方法 */ public function add() { $email = $this->data['email']; $this->data['password'] = md5(md5($this->data['password']) . $this->data['salt']); $id = parent::add(); $key = md5($email); $mail_content = "<a href='http://www.shop.com/index.php/Member/fire/id/{$id}/key/{$key}' target='_blank'>点击我激活账号</a>"; //准备发送一封激活邮件 $result = sendMail($email, '京西商城的激活邮件', $mail_content); if ($result === false) { $this->error = '发送邮件失败!'; return false; } return $id; }
/** * 留言处理 */ public function do_post() { $this->checkLogin(); $content = I('content'); if (empty($content)) { $this->error('留言内容不能为空'); } if (mb_strlen($content, 'utf-8') > 100) { $this->error('留言内容最多100字'); } $model = new Model('Message'); $userId = session('user.userId'); $data = array('content' => $content, 'created_at' => time(), 'user_id' => $userId); if (!($model->create($data) && $model->add())) { $this->error('留言失败'); } $this->success('留言成功', U('Index/index')); }
function updateDeviceToken() { $param = json_decode(file_get_contents('php://input'), true); $xtoken = $param['xtoken']; init_verify_token($xtoken); $uid = $param['uid']; if (empty($uid)) { err_ret(-205, 'lack of param', '缺少参数'); } $device_token = $param['device_token']; if (empty($device_token)) { err_ret(-205, 'lack of param', '缺少参数'); } //设备类型 1安卓手机 2苹果手机 $type = $param['type']; if ($type != 1 && $type != 2) { err_ret(-205, 'param is error', '参数错误'); } $model = new Model('apns_user'); $where_data['uid'] = $uid; $result = $model->where($where_data)->select(); if (count($result) > 0) { //如果已经有了deviceToken $old_type = $param[0]['type']; $old_device_token = $param[0]['device_token']; if ($old_device_token != $device_token) { $type = $old_type == 1 ? 2 : 1; $condition['uid'] = $uid; $save_data['device_token'] = $device_token; $save_data['type'] = $type; $model->where($condition)->save($save_data); } } else { $add_data['device_token'] = $device_token; $add_data['uid'] = $uid; $add_data['type'] = $type; $result = $model->add($add_data); } $data['errno'] = 0; $data['uid'] = $uid; $data['type'] = $type; $data['device_token'] = $device_token; echo json_encode($data); }
public function add() { //查询数据库中是否存在着用户名和邮箱 $where = array('username' => $this->data['username']); $reuslt = $this->getRow($where); if ($reuslt === false) { $this->error = '用户名已经存在'; return false; } $where1 = array('emails' => $this->data['emails']); $reuslt_1 = $this->getRow($where1); if ($reuslt_1 === false) { $this->error = '邮箱名已经存在'; return false; } //加密加盐 $this->data['password'] = md5($this->data['password'] . $this->data['salt']); return parent::add(); }
/** * 将收货地址数据添加到数据库中 或者修改数据 * @return mixed */ public function add() { $this->data['member_id'] = UID; //判断是添加还是修改 if ($id = $this->data['id']) { //修改返回id $rst = parent::save(); if ($rst !== false) { return $id; } else { return false; } } //如果添加的为默认地址,将数据库中修改为0 if ($this->data['is_default']) { $this->where('is_default=1'); parent::save(array('is_default' => 0)); } return parent::add(); }
/** * 添加购物车 * @return mixed */ public function add() { $this->data['goods_attribute_strs'] = htmlspecialchars_decode($this->data['goods_attribute_strs']); if (is_login()) { //用户登录状态 > 数据库 //判断是否有当前商品数据 > 考虑商品组合 $count = $this->where(array('goods_id' => $this->data['goods_id'], 'goods_attribute_ids' => $this->data['goods_attribute_ids']))->count(); if ($count === '0') { //直接添加到数据库中 $this->data['member_id'] = UID; //添加字段 return parent::add(); } else { //修改数量 return $this->where('goods_id=' . $this->data['goods_id'])->setInc('num', $this->data['num']); } } else { //用户未登陆 > cookie //得到以前的购物数据 $oldShopping = unserialize(cookie('shopping_car')); //检查当前商品是否在其中 $flag = false; if ($oldShopping) { foreach ($oldShopping as &$item) { //找到相同的,则加数量 if ($item['goods_id'] == $this->data['goods_id'] && $item['goods_attribute_ids'] == $this->data['goods_attribute_ids']) { $item['num'] += $this->data['num']; $flag = true; break; } unset($item); } } //cookie中无此值,追加此数据 if (!$flag) { $oldShopping[] = $this->data; } //将序列化的结果保存到cookie中 cookie('shopping_car', serialize($oldShopping)); } }
/** * 添加注册信息进入会员表 */ public function add() { //用MD5的方法和盐对密码进行加密 $this->data['password'] = md5($this->data['password'] . $this->data['salt']); //准备激活邮件的数据 $acceptmail = $this->data['email']; $title = "e商城账号激活邮件"; $mail_key = $this->data['mail_key']; //插入注册数据 $id = parent::add(); if ($id) { //准备邮件中的内容 $content = "<a href='www.shop.com" . U('activeUser', array('id' => $id, 'mail_key' => $mail_key)) . "'>点击激活账号</a>"; //寄送激活邮件 $result = sendMail($acceptmail, $title, $content); if ($result === false) { $this->error = '寄送邮件失败'; return false; } return $id; } return false; }
/** * 重写add方法,同时发送激活邮件 * @param mixed|string $requestData * @return bool|mixed */ public function add($requestData) { //1> 保存用户数据 //1.1> 得到用户的密码 $this->data['password'] = md5(md5($this->data['password']) . $this->data['salt']); //加盐加密 //1.2> 保存数据 $id = parent::add(); if ($id === false) { $this->error = '添加到数据库失败'; return false; } //2> 发送激活邮箱 //2.1> 邮件地址 $email = $requestData['email']; $url = WEB_URL . U('fire', array('id' => $id, 'key' => md5($email))); //以加密后的邮箱作为验证码 file_put_contents('11.txt', $url); //2.2> 发送邮件 $subject = '感谢您选择我们,请尽快激活'; $html = "<span><a href='{$url}'> 点击我激活账户,开始新的旅程! </a></span>"; sendEmail($email, $requestData['username'], $subject, $html); return $id; }
public function add($requestData) { $this->startTrans(); //获得购物车中的数据 $rows = D('ShoppingCar')->getList(); $price = 0; foreach ($rows as $row) { //算出购物车的总价 $price += $row['shop_price'] * $row['num']; } $file = fopen('./stock.lock', 'r+'); //创建文件,存储锁的状态 //判断商品库存是否充足 if (flock($file, LOCK_EX)) { //上锁 foreach ($rows as $row) { $one = D('Goods')->where(array('id' => $row['goods_id']), array('stock' => array('EGT', $row['num'])))->find(); if (empty($one)) { //库存不足的情况 $this->error = $row['name'] . "库存数量不足"; $this->rollback(); return false; } else { //库存数量充足的情况 $result = M('goods')->where(array('id' => $row['goods_id']))->setDec('stock', $row['num']); //修改库存 if ($result === false) { $this->error = "修改" . $row['name'] . "商品库存失败"; $this->rollback(); return false; } } } } flock($file, LOCK_UN); //解锁 fclose($file); //关闭文件 //准备订单数据 $orderinfo = array(); //>>1.根据用户的地址id找到用户地址信息 $address_id = $requestData['address_id']; $address = D('Address')->get($address_id); $orderinfo = array_merge($orderinfo, $address); //>>2.根据用户的运送id获得运送方式信息 $delivery_id = $requestData['delivery_id']; $delivery = D('Delivery')->get($delivery_id); $orderinfo = array_merge($orderinfo, $delivery); //>>3.根据用户的付款方式id,获得付款方式信息 $paytype_id = $requestData['pay_type_id']; $paytype = D('PayType')->get($paytype_id); $orderinfo = array_merge($orderinfo, $paytype); //>>4.准备会员id和会员时间 $orderinfo['member_id'] = UID; $orderinfo['inputtime'] = NOW_TIME; //>>5.准备下单时的订单状态 $orderinfo['order_status'] = 0; $orderinfo['shipping_status'] = 0; $orderinfo['pay_status'] = 0; //>>6.根据购物明细算出总价格 $orderinfo['price'] = $price + $orderinfo['delivery_price']; //明细的价格总和 与 运费的价格 //将数据添加进订单中 $order_info_id = parent::add($orderinfo); if ($order_info_id === false) { $this->error = '保存失败!'; $this->rollback(); return false; } //准备订单明细表数据 //>>1.得到购物车中的明细 $rows = D('ShoppingCar')->getList(); foreach ($rows as &$row) { $row['order_info_id'] = $order_info_id; $row['price'] = $row['shop_price']; } //>>2.放入订单明细表中 $result = D('OrderInfoItem')->addAll($rows); if ($result === false) { $this->error = "保存订单明细失败"; $this->rollback(); return false; } //准备发票数据 $invoiceinfo = array(); $invoiceinfo['order_info_id'] = $order_info_id; $invoice_type = $requestData['invoice_type']; $invoice_name = $address['receiver']; if ($invoice_type) { $invoice_name = $requestData['invoice_name']; } $invoiceinfo['name'] = $invoice_name; $invoiceinfo['content'] = $requestData['invoce_content']; $invoiceinfo['price'] = $price; $invoice_id = D('Invoice')->add($invoiceinfo); if ($invoice_id === false) { $this->error = '保存发票信息失败'; $this->rollback(); return false; } //保存发票id到对应订单中 $arr = array('id' => $order_info_id, 'invoice_id' => $invoice_id); //保存货号到订单中,货号等于:当前时间+id组成的20位数字 $sn = date('Ymd') . str_pad($order_info_id, 12, 0, STR_PAD_LEFT); $arr['sn'] = $sn; $result = parent::save($arr); if ($result === false) { $this->error = '更新订单信息失败'; $this->rollback(); return false; } $this->commit(); return $order_info_id; }
/** * 添加订单 * @param mixed|string $requestData * @return bool|mixed */ public function add($requestData) { $this->startTrans(); //为order_info表准备数据 $orderInfo = array(); $orderInfo['member_id'] = UID; //>>收货人信息 $address_id = $requestData['address_id']; $addressModel = D('Address'); $address = $addressModel->field('id,member_id,is_default,status', true)->find($address_id); $orderInfo = array_merge($orderInfo, $address); //>>送货方式 $delivery_id = $requestData['delivery']; $deliveryModel = D('Delivery'); $delivery = $deliveryModel->field('id as delivery_id,name as delivery_name,price as delivery_price')->find($delivery_id); $orderInfo = array_merge($orderInfo, $delivery); $orderInfo['delivery_time'] = $requestData['delivery_time']; //>>支付方式 $pay_type_id = $requestData['pay']; $payTypeModel = D('PayType'); $pay_type = $payTypeModel->field('id as pay_type_id,name as pay_type_name')->find($pay_type_id); $orderInfo = array_merge($orderInfo, $pay_type); //>>下单时间 $orderInfo['inputtime'] = NOW_TIME; //购物车中信息 $shoppingCarModel = D('ShoppingCar'); $shopping_car = $shoppingCarModel->getList(); //>>计算购物明细金额及订单总金额 $goods_total_price = 0; //购物明细金额 foreach ($shopping_car as $item) { $goods_total_price += $item['num'] * $item['price']; } $orderInfo['total_price'] = $goods_total_price + $delivery['delivery_price']; //保存订单信息到数据库中 $id = parent::add($orderInfo); if ($id === false) { $this->rollback(); return false; } //将购物车中的信息保存到购物明细表中 foreach ($shopping_car as &$item) { unset($item['id']); unset($item['member_id']); $item['order_info_id'] = $id; } unset($item); $orderInfoItemModel = M('OrderInfoItem'); $rst = $orderInfoItemModel->addAll($shopping_car); if ($rst === false) { $this->rollback(); $this->error = '保存订单明细失败'; return false; } //发票信息 $invoiceModel = M('Invoice'); $invoice = array(); //发票数据 //>>发票名字 if ($requestData['type'] == 1) { $userinfo = login(); $invoice_name = $userinfo['username']; } else { $invoice_name = $requestData['invoice_name']; } //>>发票相关信息 $invoice_content = ''; if ($requestData['content'] == '明细') { foreach ($shopping_car as $item) { $invoice_content .= $item['name'] . ' ' . $item['num'] . ' ' . $item['price'] . ' ' . $item['num'] * $item['price'] . "\r\n"; } } else { $invoice_content = $requestData['content']; } $invoice['name'] = $invoice_name; $invoice['content'] = $invoice_content; $invoice['order_info_id'] = $id; $invoice['total_price'] = $goods_total_price; $invoice_id = $invoiceModel->add($invoice); if ($invoice_id === false) { $this->rollback(); $this->error = '保存发票失败'; return false; } //将订单号和发票id更新到订单表中 $sn = date('Ymd') . str_pad($id, 11, '0', STR_PAD_LEFT); $rst = parent::save(array('sn' => $sn, 'invoice_id' => $invoice_id, 'id' => $id)); if ($rst === false) { $this->rollback(); $this->error = '更新订单号和发票ID失败'; return false; } $this->commit(); return $id; }
public function testAdd() { $config = $this->getConfig(); $time = time(); $user_model = new Model('test.user', $config); $data = ['username' => 'test', 'password' => md5('123456'), 'status' => 1, 'create_time' => $time]; $user_id = $user_model->data($data)->add(); $data = ['username' => 'test2', 'password' => md5('000000'), 'status' => 1, 'create_time' => $time]; $user_model->add($data, true); $data = [['user_id' => $user_id, 'consignee' => '张三', 'area_info' => '广东深圳', 'city_id' => '42', 'area_id' => '111', 'address' => 'xx路xx号', 'mobile' => '1380000000000', 'isdefault' => '1'], ['user_id' => $user_id, 'consignee' => '李四', 'area_info' => '广东深圳', 'city_id' => '42', 'area_id' => '111', 'address' => 'xx路xx号', 'mobile' => '13999999999', 'isdefault' => '0']]; $address_model = new Model('user_address', $config); $address_id = $address_model->addAll($data, [], true); $data = [['user_id' => $user_id, 'sn' => '10001', 'amount' => '200', 'freight_fee' => '10', 'address_id' => $address_id - 1, 'status' => '1', 'create_time' => $time], ['user_id' => $user_id, 'sn' => '10002', 'amount' => '350.80', 'freight_fee' => '10', 'address_id' => $address_id, 'status' => '0', 'create_time' => $time]]; $address_model = new Model('order', $config); $address_model->addAll($data); $data = ['user_id' => $user_id, 'role_id' => 1]; $config['db_name'] = 'test'; $config['attr_case'] = 2; $model = new Model('', $config); $model->table($config['prefix'] . 'role_user')->data($data)->add(); }
function weixinPay() { $param = file_get_contents('php://input'); $uid = $_REQUEST['uid']; $pid = $_REQUEST['pid']; $coachid = $_REQUEST['coachid']; $condition = array(); $condition['uid'] = $uid; $condition['pid'] = $pid; $condition['coachid'] = $coachid; $xml = simplexml_load_string($param); foreach ($xml->children() as $child) { $key = $child->getName(); if ($key == "nonce_str") { $condition['nonce_str'] = (string) $child; } else { if ($key == "out_trade_no") { $condition['out_trade_no'] = (string) $child; } else { if ($key == "transaction_id") { $condition['transaction_id'] = (string) $child; } else { if ($key == "time_end") { $condition['time_end'] = time(); } else { if ($key == "total_fee") { $condition['money'] = (int) $child; } else { if ($key == "sign") { $condition['sign'] = (string) $child; } else { if ($key == "result_code") { $result_code = (string) $child; if ($result_code == 'SUCCESS') { $condition['status'] = 1; //支付成功 } else { $condition['status'] = 2; //支付失败 } } } } } } } } } //测试 if ($condition['status'] == 1) { //发送通知 newOrderNotification($coachid, $uid); $model_order = new Model('xorder'); $count = $model_order->add($condition); $con['uid'] = $uid; $con['pid'] = $pid; $con['coachid'] = $coachid; $result = $model_order->where($con)->select(); $model_my_plan = new Model('my_plan'); if (count($result) == 1) { //第一次,添加一条记录 $first_pay_time = $result[0]['time_end']; $con['courseid'] = 0; $con['status'] = 1; $con['iscontacted'] = 0; $con['isfinished'] = 0; $con['tips'] = ''; $con['pay_time'] = $first_pay_time; $con['course_time'] = 0; $con['begin_time'] = $first_pay_time + 86400; //付款的第二天 $con['end_time'] = $first_pay_time + 86400 + count($result) * 30 * 86400; //计划的结束时间 $result_my_plan = $model_my_plan->add($con); // writeOneLine('/tmp/a.txt',"第一次添加my_plan count=$count"); } else { //不是第一次付款,更新我的计划的结束时间 $save_condition['uid'] = $uid; $save_condition['pid'] = $pid; $save_condition['coachid'] = $coachid; $result_my_plan = $model_my_plan->where($save_condition)->select(); $save_data['end_time'] = $result_my_plan[0]['begin_time'] + 86400 + count($result) * 30 * 86400; if ($result_my_plan[0]['status'] == 4) { $save_data['status'] = 3; } $result_my_plan = $model_my_plan->where($save_condition)->save($save_data); // writeOneLine('/tmp/a.txt',"不是第一次添加my_plan count=$count"); } } else { if ($condition['status'] == 2) { } } echo '<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>'; }
function addManyDayCourse() { header('Access-Control-Allow-Origin:*'); //跨域 header("Content-type: text/html; charset=utf-8"); $param = json_decode(file_get_contents('php://input'), true); /* $str = '{ "xtoken":"35dsakfsdjfcvjdsajfkdsf234", "coachid":45, "uid":999, "pid":41, "course_list":[ { "course_time":32435435, "action_list":[ { "actionid":177, "group":3, "count":20, "order":1, "type":1, "duration":34 }, { "actionid":188, "group":3, "count":20, "order":1, "type":2, "duration":54 }] }, { "course_time":32438935, "action_list":[ { "actionid":199, "group":3, "count":20, "order":1, "type":1, "duration":34 }, { "actionid":166, "group":3, "count":20, "order":1, "type":2, "duration":54 }] }] }';*/ // $param = json_decode($str,true); $token = $param['xtoken']; init_verify_token($token); $uid = $param['uid']; $pid = $param['pid']; $coachid = $param['coachid']; if (!isset($uid) || !isset($pid) || !isset($coachid)) { err_ret(-205, 'lack of param', '缺少参数'); } $course_list = $param['course_list']; //找到开始时间和结束时间 $begin_time = $course_list[0]['course_time']; $end_time = $course_list[0]['course_time']; for ($i = 0; $i < count($course_list); $i++) { if ($course_list[$i]['course_time'] <= $begin_time) { $begin_time = $course_list[$i]['course_time']; } if ($course_list[$i]['course_time'] >= $end_time) { $end_time = $course_list[$i]['course_time']; } } //课程id $model_course_record = new Model('course_record'); $cur_courseid = $model_course_record->max('courseid') + 1; //获取 pay_time $model_my_plan = new Model('my_plan'); $condition['uid'] = $uid; $condition['pid'] = $pid; $condition['coachid'] = $coachid; $condition['courseid'] = 0; $result_pay_time = $model_my_plan->select(); $pay_time = $result_pay_time[0]['pay_time']; $model_my_plan->where($condition)->delete(); //删除记录为courseid=0的 for ($i = 0; $i < count($course_list); $i++) { //添加课程记录 $action_list = $course_list[$i]['action_list']; for ($j = 0; $j < count($action_list); $j++) { $action_list[$j]['courseid'] = $cur_courseid; $model_course_record->add($action_list[$j]); } //把相应的课程记录添加到my_plan unset($data_my_plan); $data_my_plan['uid'] = $uid; $data_my_plan['pid'] = $pid; $data_my_plan['coachid'] = $coachid; $data_my_plan['courseid'] = $cur_courseid; $data_my_plan['status'] = 3; //课程已经制定 $data_my_plan['iscontacted'] = 0; $data_my_plan['isfinished'] = 0; $data_my_plan['pay_time'] = $pay_time; $data_my_plan['course_time'] = $course_list[$i]['course_time']; $data_my_plan['begin_time'] = $begin_time; $data_my_plan['end_time'] = $end_time; $model_my_plan->add($data_my_plan); $cur_courseid++; } $data['errno'] = 0; $data['uid'] = $uid; $data['pid'] = $pid; $data['coachid'] = $coachid; echo json_encode($data); }
/** * 将请求数据保持到数据库表中order_info表中 * @param mixed|string $requestData */ public function add($requestData) { $this->startTrans(); //>>1.通过请求数据为order_info表中准备数据 //>>1.1 准备订单的用户 $orderInfo = array(); $orderInfo['member_id'] = UID; //>>1.2 订单的总价格 (购物车中的总价格+运费) $price = 0; $shoppingCarModel = D('ShoppingCar'); $shoppingCar = $shoppingCarModel->getListData(); foreach ($shoppingCar as $shoppingCar_item) { $price += $shoppingCar_item['num'] * $shoppingCar_item['shop_price']; } ////////////////////////判断购物车中的商品在库存中是否存在,并且减少库存 开始//////////////////////////// //>>1.打开一个文件.让文件作为锁 $file = fopen('./xxx.lock', 'r'); //>>2.加锁 if (flock($file, LOCK_EX)) { foreach ($shoppingCar as $shoppingCar_item) { //>>1.判断单库存单价格的库存(goods表中stock) if (empty($shoppingCar_item['goods_attribute_ids'])) { $wheres = "id={$shoppingCar_item['goods_id']} and stock>={$shoppingCar_item['num']}"; $goodsModel = M('Goods'); $row = $goodsModel->field('id')->where($wheres)->find(); if ($row) { //如果查询出来说明有库存,需要减少库存 $result = $goodsModel->where($wheres)->setDec('stock', $shoppingCar_item['num']); if ($result === false) { $this->error = '减少库存失败!'; $this->rollback(); return false; } } else { $this->error = '库存不足!'; $this->rollback(); return false; } } else { //>>2.判断多库存多价格的库存(product表中stock字段上) $productModel = M('product'); $wheres = "goods_id={$shoppingCar_item['goods_id']} and goods_attribute_ids='{$shoppingCar_item['goods_attribute_ids']}' and stock>={$shoppingCar_item['num']}"; $row = $productModel->where($wheres)->find(); if ($row) { //如果查询出来说明有库存,需要减少库存 $result = $productModel->where($wheres)->setDec('stock', $shoppingCar_item['num']); if ($result === false) { $this->error = '减少库存失败!'; $this->rollback(); return false; } } else { $this->error = '库存不足!'; $this->rollback(); return false; } } } flock($file, LOCK_UN); } fclose($file); ////////////////////////判断购物车中的商品在库存中是否存在,并且减少库存 接收//////////////////////////// //根据送货方式的id,得到价格 $deliveryModel = M('Delivery'); $deliveryPrice = $deliveryModel->getFieldById($requestData['delivery_id'], 'price'); $orderInfo['price'] = $price + $deliveryPrice; //包含商品价格和运费 $orderInfo['add_time'] = NOW_TIME; //添加时间 $orderInfo['delivery_price'] = $deliveryPrice; //运费 //>>1.3 收货地址 $addressModel = M('Address'); $address = $addressModel->field('name,province_id,city_id,area_id,detail_address,tel')->find($requestData['address_id']); $orderInfo = array_merge($orderInfo, $address); //>>1.4 送货方式 $orderInfo['delivery_id'] = $requestData['delivery_id']; //>>1.5.支付方式 $orderInfo['pay_type'] = $requestData['pay_type']; //>>2.保持orderInfo中的数据到order_info表中 $id = parent::add($orderInfo); if ($id === false) { $this->rollback(); return false; } //>>5.保持订单明细(从购物车中得到数据保持到订单明细表中) $orderinfoItems = array(); //将购物车中的每个明细转换为订单的每个明细,然后保存明细表中 foreach ($shoppingCar as $item) { $orderinfoItem = array('goods_id' => $item['goods_id'], 'goods_name' => $item['name'], 'goods_logo' => $item['logo'], 'goods_shop_price' => $item['shop_price'], 'num' => $item['num'], 'price' => $item['shop_price'], 'order_info_id' => $id); if (!empty($item['goods_attribute_ids'])) { $orderinfoItem['goods_attribute_ids'] = $item['goods_attribute_ids']; $orderinfoItem['goods_attribute_strs'] = $item['goods_attribute_strs']; } else { $orderinfoItem['goods_attribute_ids'] = ''; $orderinfoItem['goods_attribute_strs'] = ''; } $orderinfoItems[] = $orderinfoItem; } $oderInfoItemModel = M('OrderInfoItem'); if (!empty($orderinfoItems)) { $result = $oderInfoItemModel->addAll($orderinfoItems); if ($result === false) { $this->rollback(); $this->error = '保存订单明细失败!'; return false; } } //>>6.保持发票数据 $invoice = $requestData['invoice']; //>>6.1准备发票的名字 switch ($invoice['type']) { case 0: $userinfo = login(); $invoice_name = $userinfo['username']; break; case 1: $invoice_name = $invoice['name']; break; } //>>6.2 准备发票的内容 $content = $invoice['content']; if ($content === "1") { //如果是1, 表示需要得到用户的购买详细信息作为 发票的明细 $content = ''; foreach ($shoppingCar as $item) { $total_price = $item['num'] * $item['shop_price']; $content .= "{$item['name']} {$item['num']} {$item['shop_price']} {$total_price}<br/>"; } } $invoice = array('name' => $invoice_name, 'content' => $content, 'price' => $price, 'order_info_id' => $id); $invoiceModel = M('Invoice'); $invoice_id = $invoiceModel->add($invoice); if ($invoice_id === false) { $this->rollback(); $this->error = '保持发票失败!'; return false; } //>>3.计算出订单号 2015072 0000000001 $sn = date("YmdHis") . str_pad($id, 10, '0', STR_PAD_LEFT); //>>4.将订单号更新到当前的订单中 $result = parent::save(array('id' => $id, 'sn' => $sn, 'invoice_id' => $invoice_id)); if ($result === false) { $this->error = '更新订单号或者发票失败!'; $this->rollback(); return false; } $this->commit(); return $sn; //返回订单 }
function register() { $param = json_decode(file_get_contents('php://input'), true); //用户名 $username = $param['username']; if (empty($username)) { err_ret(-205, 'lack of param', '缺少参数'); } //密码 $password = $param['password']; if (empty($password)) { err_ret(-205, 'lack of param', '缺少参数'); } //昵称 $nicker = $param['nicker']; if (empty($nicker)) { err_ret(-205, 'lack of param', '缺少参数'); } $gender = $param['gender']; if ($gender != 0 && $gender != 1) { $gender = 0; //默认 男 } //头像 $header = $param['header']; if (empty($header)) { $header = ''; } //短信验证码 $verifycode = $param['verifycode']; if (empty($verifycode)) { err_ret(-306, 'lack of param verfiy', '验证码不能为空'); } //注册时间 $regtime = time(); $model = new Model('user_info'); //判断手机号是否注册过 $data['username'] = $username; $result = $model->where($data)->select(); if (count($result) > 0) { err_ret(-305, 'phone number is registered', '手机号已经注册过'); } //数据库插入一条记录,生成新用户 $data['username'] = $username; $data['password'] = $password; $data['nicker'] = $nicker; $data['header'] = $header; $data['regtime'] = $regtime; $data['gender'] = $gender; $lastId = $model->add($data); if (!$lastId) { err_ret(-311, 'register add new user failed', '注册添加新用户时失败'); } //删除此用户临时短信验证码 $delete_model = new Model('verify_tmp'); $condition['phone'] = $username; $condition['verifycode'] = $verifycode; $delete_model->where($condition)->delete(); // 删除id为最大的用户的短信验证码 //生成用户token并保存 $token = token_generate($lastId); $where['id'] = $lastId; $save['xtoken'] = $token; $count = $model->where($where)->save($save); if ($count == 0) { err_ret(-501, 'save token failed', '保存token失败'); } //注册环信 Vendor('EasemobApi.EasemobApi'); $ease = new \Easemob(); $result_arr = $ease->registerUser($username, $password, $nicker); if (isset($result_arr['error'])) { $delete_data['username'] = $username; $model->where($delete_data)->delete(); err_ret(-205, 'failed registered', '注册失败'); } $info['errno'] = 0; $info['xtoken'] = $token; $info['data']['nicker'] = $nicker; $info['data']['header'] = $header; $info['data']['uid'] = $lastId; echo json_encode($info); }
/** * 添加订单 * @param mixed|string $requestData * @return bool|mixed */ public function add($requestData) { $this->startTrans(); //1>准备数据 $data = $requestData; //1.1>准备收货地址信息 $addresse = M('Address')->field('id,is_default,status', true)->where('id=' . $data['address_id'])->find(); $data += $addresse; //1.2>准备送货方式信息 $delivery = M('Delivery')->field('name as delivery_name,price as delivery_price')->where('id=' . $data['delivery_id'])->find(); $data += $delivery; //1.3>准备支付方式信息 $payType = M('PayType')->field('name as pay_type_name')->where('id=' . $data['pay_type_id'])->find(); $data += $payType; //1.4>商品--购物车--总价 $shoppingCar = D('ShoppingCar'); $shoppingCars = $shoppingCar->getGoodsInfo(); $shoppingCar->where('member_id=' . UID)->delete(); //清空此用户的购物车信息 $total = 0; //不含运费的价格 foreach ($shoppingCars as $item) { $total += $item['price'] * $item['num']; } $trueTotal = $total + $data['delivery_price']; //包含运费的价格 $data['total_price'] = $trueTotal; //1.5>订单生产时间 $data['inputtime'] = NOW_TIME; //1.6>添加到数据库 $id = parent::add($data); if ($id === false) { $this->rollback(); return false; } //2>添加商品发票信息 $invoice = array(); //2.1>发票名 if ($data['invoice_type'] == 1) { $user = login(); $invoice['name'] = $user['username']; } else { $invoice['name'] = $data['invoice_name']; } //2.2>发票内容 if ($data['invoice_content'] == '明细') { $content = ''; foreach ($shoppingCars as $item) { $content .= $item['name'] . ' ' . $item['price'] . ' ' . $item['num'] . "\r\n"; } $invoice['content'] = $content; } else { $invoice['content'] = $data['invoice_content']; } //2.3>发票total_price $invoice['total_price'] = $total; //2.4>发票订单号 $invoice['order_info_id'] = $id; //2.5>添加发票 $invoice_id = M('Invoice')->add($invoice); if ($invoice_id === false) { $this->rollback(); $this->error = '添加发票失败'; return false; } //3>添加商品详情信息 foreach ($shoppingCars as &$item) { //添加订单id $item['order_info_id'] = $id; //更新库存 if ($item['goods_attribute_ids']) { //多库存 //文件锁,仅允许一个用户操作 $fp = fopen("lock.php", "r"); if (flock($fp, LOCK_EX)) { // 进行排它型锁定,独占锁 // 实际库存数量 $stock = M('Product')->where(array('goods_attribute_ids' => $item['goods_attribute_ids']))->getFieldByGoodsId($item['goods_id'], 'stock'); // 检测库存,并处理 if ($stock >= $item['num']) { //减库存 $rst = M('Product')->where(array('goods_attribute_ids' => $item['goods_attribute_ids']))->setDec('stock', $item['num']); if ($rst === false) { $this->rollback(); $this->error = $item['name'] . '库存更新失败' . M('Product')->getLastSql(); return false; } } else { $this->rollback(); $this->error = $item['name'] . ',库存不足'; return false; } flock($fp, LOCK_UN); // 释放锁定 } fclose($fp); //释放文件资源 } else { //单库存 //文件锁,仅允许一个用户操作 $fp = fopen("lock.php", "r"); if (flock($fp, LOCK_EX)) { // 进行排它型锁定,独占锁 // 实际库存数量 $stock = M('Goods')->getFieldById($item['goods_id'], 'stock'); // 检测库存,并处理 if ($stock >= $item['num']) { //减库存 $rst = M('Goods')->where('id=' . $item['goods_id'])->setDec('stock', $item['num']); if ($rst === false) { $this->rollback(); $this->error = $item['name'] . '库存更新失败' . M('Goods')->getLastSql(); return false; } } else { $this->rollback(); $this->error = $item['name'] . ',库存不足'; return false; } flock($fp, LOCK_UN); // 释放锁定 } fclose($fp); //释放文件资源 } } unset($item); $order_info_item_id = M('OrderInfoItem')->addAll($shoppingCars); if ($order_info_item_id === false) { $this->rollback(); $this->error = '添加订单明细失败'; return false; } //4>更新商品信息>sn,invoice_id $sn = date('Ymd') . str_pad($id, 11, '0', STR_PAD_LEFT); $rst = $this->where('id=' . $id)->save(array('sn' => $sn, 'invoice_id' => $invoice_id)); if ($rst === false) { $this->rollback(); $this->error = '修改订单失败'; return false; } $this->commit(); return $id; }